From b7339a72fb6d8e8e00822ef1e0166628553a8dfd Mon Sep 17 00:00:00 2001 From: Li Jie Date: Mon, 27 Jul 2026 12:42:46 +0800 Subject: [PATCH 01/18] runtime/wasm: add single-worker Asyncify scheduler --- internal/build/build.go | 7 +- internal/build/outputs.go | 6 + internal/crosscompile/crosscompile.go | 2 +- .../internal/clite/emscripten/_wrap/fiber.c | 5 + runtime/internal/clite/emscripten/fiber.go | 42 +++ .../internal/clite/emscripten/fiber_wasm.go | 21 ++ runtime/internal/lib/runtime/debug.go | 3 + runtime/internal/runqueue/runqueue.go | 75 +++++ runtime/internal/runtime/g_tls.go | 2 +- runtime/internal/runtime/g_wasm.go | 32 ++ runtime/internal/runtime/os_pthread.go | 7 +- runtime/internal/runtime/os_wasm.go | 23 ++ runtime/internal/runtime/proc.go | 141 ++------- runtime/internal/runtime/proc_atomic.go | 6 +- runtime/internal/runtime/proc_pthread.go | 127 ++++++++ runtime/internal/runtime/proc_wasm.go | 279 ++++++++++++++++++ runtime/internal/runtime/runtime2.go | 32 +- runtime/internal/runtime/z_default.go | 9 +- 18 files changed, 681 insertions(+), 138 deletions(-) create mode 100644 runtime/internal/clite/emscripten/_wrap/fiber.c create mode 100644 runtime/internal/clite/emscripten/fiber.go create mode 100644 runtime/internal/clite/emscripten/fiber_wasm.go create mode 100644 runtime/internal/runqueue/runqueue.go create mode 100644 runtime/internal/runtime/g_wasm.go create mode 100644 runtime/internal/runtime/os_wasm.go create mode 100644 runtime/internal/runtime/proc_pthread.go create mode 100644 runtime/internal/runtime/proc_wasm.go diff --git a/internal/build/build.go b/internal/build/build.go index 6db6ae9085..81cbb024ea 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -793,11 +793,8 @@ func DefaultBuildTags(goarch, target string) string { func defaultBuildTags(goarch, target string) string { tags := "llgo,math_big_pure_go,purego" - // Raw GOOS/GOARCH wasm builds do not have a target configuration that - // selects a collector. BDWGC is not available in either wasm host, so use - // the supported collector-free runtime unless a named target supplies its - // own runtime configuration. - if goarch == "wasm" && target == "" { + // BDWGC is unavailable in both wasm hosts. + if goarch == "wasm" { tags += ",nogc" } return tags diff --git a/internal/build/outputs.go b/internal/build/outputs.go index 4ab63186e7..3553fbb049 100644 --- a/internal/build/outputs.go +++ b/internal/build/outputs.go @@ -279,6 +279,12 @@ func defaultAppExt(conf *Config) string { return ".so" } case BuildModeExe: + if conf.Goos == "js" && conf.OutFile != "" { + switch ext := filepath.Ext(conf.OutFile); ext { + case ".js", ".mjs": + return ext + } + } // For executable mode, handle target-specific logic if conf.Target != "" { if strings.HasPrefix(conf.Target, "wasi") || strings.HasPrefix(conf.Target, "wasm") { diff --git a/internal/crosscompile/crosscompile.go b/internal/crosscompile/crosscompile.go index c017c7e5f4..614ac0eeae 100644 --- a/internal/crosscompile/crosscompile.go +++ b/internal/crosscompile/crosscompile.go @@ -445,7 +445,7 @@ func use(goos, goarch string, wasiThreads, forceEspClang bool, level optlevel.Le // "-Wl,--export=malloc", "-Wl,--export=free", } export.LDFLAGS = append(export.LDFLAGS, []string{ - "-sENVIRONMENT=web,worker", + "-sENVIRONMENT=web,worker,node", "-DPLATFORM_WEB", "-sEXPORT_KEEPALIVE=1", "-sEXPORT_ES6=1", diff --git a/runtime/internal/clite/emscripten/_wrap/fiber.c b/runtime/internal/clite/emscripten/_wrap/fiber.c new file mode 100644 index 0000000000..43a99f8fb5 --- /dev/null +++ b/runtime/internal/clite/emscripten/_wrap/fiber.c @@ -0,0 +1,5 @@ +#include + +_Static_assert( + sizeof(emscripten_fiber_t) == 8 * sizeof(void *), + "LLGo Fiber storage does not match emscripten_fiber_t"); diff --git a/runtime/internal/clite/emscripten/fiber.go b/runtime/internal/clite/emscripten/fiber.go new file mode 100644 index 0000000000..8490f8fd10 --- /dev/null +++ b/runtime/internal/clite/emscripten/fiber.go @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package emscripten exposes the small host ABI needed by the WebAssembly +// execution-context backend. +package emscripten + +import c "github.com/goplus/llgo/runtime/internal/clite" + +// Fiber is the opaque emscripten_fiber_t storage. The C layout consists of +// eight pointer-sized fields on wasm32. +type Fiber struct { + _ [8]uintptr +} + +//llgo:type C +type FiberEntry func(c.Pointer) + +// llgo:link (*Fiber).Init C.emscripten_fiber_init +func (fiber *Fiber) Init(entry FiberEntry, arg, stack c.Pointer, stackSize uintptr, asyncifyStack c.Pointer, asyncifyStackSize uintptr) { +} + +// llgo:link (*Fiber).InitCurrent C.emscripten_fiber_init_from_current_context +func (fiber *Fiber) InitCurrent(asyncifyStack c.Pointer, asyncifyStackSize uintptr) { +} + +// llgo:link (*Fiber).Swap C.emscripten_fiber_swap +func (fiber *Fiber) Swap(next *Fiber) { +} diff --git a/runtime/internal/clite/emscripten/fiber_wasm.go b/runtime/internal/clite/emscripten/fiber_wasm.go new file mode 100644 index 0000000000..2a7060a4a1 --- /dev/null +++ b/runtime/internal/clite/emscripten/fiber_wasm.go @@ -0,0 +1,21 @@ +//go:build js && wasm + +/* + * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package emscripten + +const LLGoFiles = "_wrap/fiber.c" diff --git a/runtime/internal/lib/runtime/debug.go b/runtime/internal/lib/runtime/debug.go index b19cb2b9d2..f8d832d59b 100644 --- a/runtime/internal/lib/runtime/debug.go +++ b/runtime/internal/lib/runtime/debug.go @@ -1,5 +1,7 @@ package runtime +import llruntime "github.com/goplus/llgo/runtime/internal/runtime" + func NumCPU() int { return int(c_maxprocs()) } @@ -9,6 +11,7 @@ func Breakpoint() { } func Gosched() { + llruntime.Gosched() } func NumCgoCall() int64 { diff --git a/runtime/internal/runqueue/runqueue.go b/runtime/internal/runqueue/runqueue.go new file mode 100644 index 0000000000..1e6f32c57c --- /dev/null +++ b/runtime/internal/runqueue/runqueue.go @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package runqueue provides an allocation-free intrusive FIFO for scheduler +// backends with one queue owner. +package runqueue + +// Node is the intrusive link contract implemented by scheduler-owned values. +type Node[T comparable] interface { + RunqueueNext() T + SetRunqueueNext(T) + RunqueueQueued() bool + SetRunqueueQueued(bool) +} + +type Queue[T interface { + comparable + Node[T] +}] struct { + head T + tail T + size uintptr +} + +// Push appends node and reports whether it was non-zero and not queued. +func (q *Queue[T]) Push(node T) bool { + var zero T + if node == zero || node.RunqueueQueued() { + return false + } + node.SetRunqueueNext(zero) + node.SetRunqueueQueued(true) + if q.tail == zero { + q.head = node + } else { + q.tail.SetRunqueueNext(node) + } + q.tail = node + q.size++ + return true +} + +// Pop removes and returns the oldest node, or its zero value when empty. +func (q *Queue[T]) Pop() T { + var zero T + node := q.head + if node == zero { + return zero + } + q.head = node.RunqueueNext() + if q.head == zero { + q.tail = zero + } + node.SetRunqueueNext(zero) + node.SetRunqueueQueued(false) + q.size-- + return node +} + +func (q *Queue[T]) Len() uintptr { + return q.size +} diff --git a/runtime/internal/runtime/g_tls.go b/runtime/internal/runtime/g_tls.go index 9a8bcc5262..7300e4be6e 100644 --- a/runtime/internal/runtime/g_tls.go +++ b/runtime/internal/runtime/g_tls.go @@ -1,4 +1,4 @@ -//go:build llgo && !baremetal +//go:build llgo && !baremetal && (!js || !wasm) /* * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. diff --git a/runtime/internal/runtime/g_wasm.go b/runtime/internal/runtime/g_wasm.go new file mode 100644 index 0000000000..9ec5a4f221 --- /dev/null +++ b/runtime/internal/runtime/g_wasm.go @@ -0,0 +1,32 @@ +//go:build llgo && js && wasm + +/* + * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package runtime + +var currentG *g + +func getg() *g { + if currentG == nil { + currentG = initRuntimeContext(allocRuntimeContext(), nil, _Grunning) + } + return currentG +} + +func setg(gp *g) { + currentG = gp +} diff --git a/runtime/internal/runtime/os_pthread.go b/runtime/internal/runtime/os_pthread.go index 4a7447fda4..7d2585a1b3 100644 --- a/runtime/internal/runtime/os_pthread.go +++ b/runtime/internal/runtime/os_pthread.go @@ -1,3 +1,5 @@ +//go:build !llgo || !js || !wasm + /* * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. * @@ -63,8 +65,7 @@ func initThreadAttr(attr *pthread.Attr, stackSize uintptr) c.Int { return 0 } -func exitCurrentM() { - mp := getg().m - mexit(mp) +func goexitBackend(gp *g) { + mexit(gp.m) pthread.Exit(nil) } diff --git a/runtime/internal/runtime/os_wasm.go b/runtime/internal/runtime/os_wasm.go new file mode 100644 index 0000000000..956031b417 --- /dev/null +++ b/runtime/internal/runtime/os_wasm.go @@ -0,0 +1,23 @@ +//go:build llgo && js && wasm + +/* + * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package runtime + +// mOS is empty for the single-worker WebAssembly backend. The host Worker is +// owned by Emscripten rather than created for an individual M. +type mOS struct{} diff --git a/runtime/internal/runtime/proc.go b/runtime/internal/runtime/proc.go index 12dc6169c2..1525f7fdcd 100644 --- a/runtime/internal/runtime/proc.go +++ b/runtime/internal/runtime/proc.go @@ -28,17 +28,14 @@ import ( //llgo:type C type goroutineFunc func(unsafe.Pointer) unsafe.Pointer -// runtimeContext keeps the G, M, and P for the current 1:1 backend in one -// allocation. Keeping their ownership together makes mexit deterministic while -// leaving the individual objects and links compatible with a later M:N backend. +// runtimeContext owns one G and its target-specific suspended execution state. +// M and P ownership belongs to the selected scheduler backend and can outlive, +// or be shared by, multiple runtime contexts. type runtimeContext struct { g g - m m - p p - // root is non-nil for contexts passed through a host-thread API. Such - // contexts must remain visible to the collector until mexit. - root unsafe.Pointer + root unsafe.Pointer + platform runtimeContextPlatform } var sched struct { @@ -58,19 +55,11 @@ var sched struct { // lowering, this ABI contains no pthread types: the selected runtime backend // decides how to provide an M and execute the G. func NewProc(fn goroutineFunc, arg unsafe.Pointer, stackSize uintptr) { - gp := newproc1(fn, arg, getg()) - if errno := newm(gp.m, stackSize); errno != 0 { - ctx := gp.context - releaseG() - FreeRoot(arg) - FreeRoot(ctx.root) - panic("runtime: failed to create new OS thread") - } + newprocBackend(fn, arg, stackSize, getg()) } -// newproc1 creates a runnable G and its initial M/P ownership. The pthread -// backend starts that G immediately; a future scheduler can enqueue the same G -// without changing the compiler ABI. +// newproc1 creates target-independent runnable G state. The selected backend +// attaches execution resources and either starts or queues it. func newproc1(fn goroutineFunc, arg unsafe.Pointer, callergp *g) *g { if fn == nil { panic("go of nil func value") @@ -84,7 +73,9 @@ func newproc1(fn goroutineFunc, arg unsafe.Pointer, callergp *g) *g { } func allocRuntimeContext() *runtimeContext { - size := unsafe.Sizeof(runtimeContext{}) + // LLVM rounds contexts containing 64-bit IDs to this boundary on wasm. + const contextAlignment = uintptr(unsafe.Sizeof(uint64(0))) + size := (unsafe.Sizeof(runtimeContext{}) + contextAlignment - 1) &^ (contextAlignment - 1) root := AllocRoot(size) if root == nil { panic("runtime: failed to allocate goroutine context") @@ -95,63 +86,25 @@ func allocRuntimeContext() *runtimeContext { return ctx } -// newm starts the platform execution resource for mp. -func newm(mp *m, stackSize uintptr) int { - return newosproc(mp, stackSize) -} - -// mstart is the first LLGo runtime function executed on a new M. -func mstart(arg unsafe.Pointer) unsafe.Pointer { - mp := (*m)(arg) - if mp == nil || mp.curg == nil || mp.p == nil { - fatal("runtime: invalid mstart context") - return nil - } - gp := mp.curg - pp := mp.p - - setg(gp) - casgstatus(gp, _Grunnable, _Grunning) - setpstatus(pp, _Prunning) - - fn, arg := gp.startfn, gp.startarg - gp.startfn = nil - gp.startarg = nil - ret := fn(arg) - mexit(mp) - return ret -} - -// mexit tears down the current 1:1 G/M/P context. It does not terminate the -// host thread so both a returning start routine and runtime.Goexit can share -// the same ownership cleanup. -func mexit(mp *m) { - if mp == nil || mp.curg == nil || mp.p == nil { - fatal("runtime: invalid mexit context") +func freeRuntimeContext(ctx *runtimeContext) { + if ctx == nil || ctx.root == nil { return } - gp := mp.curg - pp := mp.p - ctx := gp.context root := ctx.root - ownedByLifecycle := currentGUsesLifecycle() - if !ownedByLifecycle { - releaseGAndCheckDeadlock() - } - - casgstatus(gp, _Grunning, _Gdead) - setpstatus(pp, _Pdead) - - pp.m = nil - mp.p = nil - mp.curg = nil - gp.m = nil + ctx.root = nil + FreeRoot(root) +} - setg(nil) - if !ownedByLifecycle && root != nil { - ctx.root = nil - FreeRoot(root) +func initG(ctx *runtimeContext, callergp *g, status uint32) *g { + gp := &ctx.g + gp.atomicstatus = status + gp.goid = nextGoid(gp) + if callergp != nil { + gp.parentGoid = callergp.goid } + gp.context = ctx + retainG() + return gp } // releaseGAndCheckDeadlock is the sole last-goroutine decision. Main marks its @@ -165,47 +118,9 @@ func releaseGAndCheckDeadlock() { } } -func initRuntimeContext(ctx *runtimeContext, callergp *g, status uint32) *g { - gp := &ctx.g - mp := &ctx.m - pp := &ctx.p - - gp.m = mp - gp.atomicstatus = status - gp.goid = nextGoid(gp) - if callergp != nil { - gp.parentGoid = callergp.goid - } - gp.context = ctx - - mp.curg = gp - mp.p = pp - mp.id = nextMid(mp) - - pp.id = nextPid(pp) - pstatus := uint32(_Pidle) - if status == _Grunning { - pstatus = _Prunning - } - setpstatus(pp, pstatus) - pp.m = mp - retainG() - return gp -} - -// GMPForTesting reports the current runtime ownership graph. It is kept -// internal to the compiler runtime and linked only by LLGo execution tests. -func GMPForTesting() (goid, parentGoid uint64, mid int64, pid int32, gstatus, pstatus uint32, linked bool) { - gp := getg() - if gp == nil || gp.m == nil || gp.m.p == nil { - return - } - mp := gp.m - pp := mp.p - ctx := gp.context - return gp.goid, gp.parentGoid, mp.id, pp.id, readgstatus(gp), readpstatus(pp), - mp.curg == gp && pp.m == mp && ctx != nil && - &ctx.g == gp && &ctx.m == mp && &ctx.p == pp +// Gosched yields the processor, allowing another goroutine to run. +func Gosched() { + goschedBackend() } // GStateForTesting reports the packed scheduler state without changing it. diff --git a/runtime/internal/runtime/proc_atomic.go b/runtime/internal/runtime/proc_atomic.go index 5db811ab3e..1028c5a78b 100644 --- a/runtime/internal/runtime/proc_atomic.go +++ b/runtime/internal/runtime/proc_atomic.go @@ -26,15 +26,15 @@ const ( ) func nextGoid(gp *g) uint64 { - return atomic.Add(&sched.goidgen, uint64(1)) + return atomic.Add(&sched.goidgen, uint64(1)) + 1 } func nextMid(mp *m) int64 { - return atomic.Add(&sched.midgen, int64(1)) + return atomic.Add(&sched.midgen, int64(1)) + 1 } func nextPid(pp *p) int32 { - return atomic.Add(&sched.pidgen, int32(1)) - 1 + return atomic.Add(&sched.pidgen, int32(1)) } func retainG() { diff --git a/runtime/internal/runtime/proc_pthread.go b/runtime/internal/runtime/proc_pthread.go new file mode 100644 index 0000000000..5aba69c90b --- /dev/null +++ b/runtime/internal/runtime/proc_pthread.go @@ -0,0 +1,127 @@ +//go:build !llgo || !js || !wasm + +/* + * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package runtime + +import "unsafe" + +// The pthread backend keeps its one-to-one M/P pair in the G context without +// exposing those fields to other execution-context backends. +type runtimeContextPlatform struct { + m m + p p +} + +func newprocBackend(fn goroutineFunc, arg unsafe.Pointer, stackSize uintptr, callergp *g) { + gp := newproc1(fn, arg, callergp) + if errno := newm(gp.m, stackSize); errno != 0 { + releaseG() + FreeRoot(arg) + freeRuntimeContext(gp.context) + panic("runtime: failed to create new OS thread") + } +} + +func initRuntimeContext(ctx *runtimeContext, callergp *g, status uint32) *g { + gp := initG(ctx, callergp, status) + mp := &ctx.platform.m + pp := &ctx.platform.p + + gp.m = mp + mp.curg = gp + mp.p = pp + mp.id = nextMid(mp) + + pp.id = nextPid(pp) + pstatus := uint32(_Pidle) + if status == _Grunning { + pstatus = _Prunning + } + setpstatus(pp, pstatus) + pp.m = mp + return gp +} + +func newm(mp *m, stackSize uintptr) int { + return newosproc(mp, stackSize) +} + +func mstart(arg unsafe.Pointer) unsafe.Pointer { + mp := (*m)(arg) + if mp == nil || mp.curg == nil || mp.p == nil { + fatal("runtime: invalid mstart context") + return nil + } + gp := mp.curg + pp := mp.p + + setg(gp) + casgstatus(gp, _Grunnable, _Grunning) + setpstatus(pp, _Prunning) + + fn, arg := gp.startfn, gp.startarg + gp.startfn = nil + gp.startarg = nil + ret := fn(arg) + mexit(mp) + return ret +} + +func mexit(mp *m) { + if mp == nil || mp.curg == nil || mp.p == nil { + fatal("runtime: invalid mexit context") + return + } + gp := mp.curg + pp := mp.p + ctx := gp.context + ownedByLifecycle := currentGUsesLifecycle() + if !ownedByLifecycle { + releaseGAndCheckDeadlock() + } + + casgstatus(gp, _Grunning, _Gdead) + setpstatus(pp, _Pdead) + + pp.m = nil + mp.p = nil + mp.curg = nil + gp.m = nil + + setg(nil) + if !ownedByLifecycle { + freeRuntimeContext(ctx) + } +} + +func goschedBackend() { +} + +// GMPForTesting reports the current runtime ownership graph. +func GMPForTesting() (goid, parentGoid uint64, mid int64, pid int32, gstatus, pstatus uint32, linked bool) { + gp := getg() + if gp == nil || gp.m == nil || gp.m.p == nil { + return + } + mp := gp.m + pp := mp.p + ctx := gp.context + return gp.goid, gp.parentGoid, mp.id, pp.id, readgstatus(gp), readpstatus(pp), + mp.curg == gp && pp.m == mp && ctx != nil && + &ctx.g == gp && &ctx.platform.m == mp && &ctx.platform.p == pp +} diff --git a/runtime/internal/runtime/proc_wasm.go b/runtime/internal/runtime/proc_wasm.go new file mode 100644 index 0000000000..184c00f7e6 --- /dev/null +++ b/runtime/internal/runtime/proc_wasm.go @@ -0,0 +1,279 @@ +//go:build llgo && js && wasm + +/* + * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package runtime + +import ( + "unsafe" + + "github.com/goplus/llgo/runtime/internal/clite/emscripten" + "github.com/goplus/llgo/runtime/internal/runqueue" +) + +const ( + defaultWasmGStackSize = 64 << 10 + defaultWasmAsyncifyStackSize = 64 << 10 +) + +type runtimeContextPlatform struct { + fiber emscripten.Fiber + stack unsafe.Pointer + asyncifyStack unsafe.Pointer +} + +var wasmSched struct { + m m + p p + runq runqueue.Queue[*g] + retired *runtimeContext + started bool +} + +func initRuntimeContext(ctx *runtimeContext, callergp *g, status uint32) *g { + gp := initG(ctx, callergp, status) + if status == _Grunning { + initWasmScheduler(gp) + } + return gp +} + +func initWasmScheduler(gp *g) { + if wasmSched.started { + fatal("runtime: WebAssembly scheduler initialized twice") + return + } + wasmSched.started = true + mp := &wasmSched.m + pp := &wasmSched.p + mp.curg = gp + mp.p = pp + mp.id = nextMid(mp) + pp.id = nextPid(pp) + setpstatus(pp, _Prunning) + pp.m = mp + gp.m = mp +} + +func newprocBackend(fn goroutineFunc, arg unsafe.Pointer, stackSize uintptr, callergp *g) { + gp := newproc1(fn, arg, callergp) + initWasmFiber(gp, stackSize) + if !wasmSched.runq.Push(gp) { + fatal("runtime: invalid run queue insertion") + } +} + +func initWasmFiber(gp *g, stackSize uintptr) { + if stackSize == 0 { + stackSize = defaultWasmGStackSize + } + stackSize = alignWasmStackSize(stackSize) + asyncifySize := uintptr(defaultWasmAsyncifyStackSize) + if stackSize > asyncifySize { + asyncifySize = stackSize + } + + platform := &gp.context.platform + platform.stack = allocWasmStack(stackSize) + platform.asyncifyStack = allocWasmStack(asyncifySize) + platform.fiber.Init( + emscripten.FiberEntry(wasmGStart), + unsafe.Pointer(gp), + platform.stack, + stackSize, + platform.asyncifyStack, + asyncifySize, + ) +} + +func alignWasmStackSize(size uintptr) uintptr { + const alignment = uintptr(16) + return (size + alignment - 1) &^ (alignment - 1) +} + +func allocWasmStack(size uintptr) unsafe.Pointer { + stack := AllocRoot(size) + if stack == nil { + panic("runtime: failed to allocate WebAssembly goroutine stack") + } + return stack +} + +func ensureCurrentWasmFiber(gp *g) { + platform := &gp.context.platform + if platform.asyncifyStack != nil { + return + } + platform.asyncifyStack = allocWasmStack(defaultWasmAsyncifyStackSize) + platform.fiber.InitCurrent(platform.asyncifyStack, defaultWasmAsyncifyStackSize) +} + +func wasmGStart(arg unsafe.Pointer) { + gp := (*g)(arg) + if gp == nil || getg() != gp { + fatal("runtime: invalid WebAssembly goroutine entry") + return + } + reapRetiredWasmG() + fn, fnarg := gp.startfn, gp.startarg + gp.startfn = nil + gp.startarg = nil + fn(fnarg) + goexitBackend(gp) +} + +func goschedBackend() { + gp := getg() + casgstatus(gp, _Grunning, _Grunnable) + if !wasmSched.runq.Push(gp) { + fatal("runtime: invalid run queue insertion") + return + } + next := popWasmRunq() + if next == gp { + casgstatus(gp, _Grunnable, _Grunning) + return + } + resumeWasmG(gp, next) +} + +func gopark() { + gp := getg() + casgstatus(gp, _Grunning, _Gwaiting) + next := popWasmRunq() + if next == nil { + fatal("all goroutines are asleep - deadlock!") + return + } + resumeWasmG(gp, next) +} + +func goready(gp *g) { + if gp == nil { + fatal("runtime: ready of nil goroutine") + return + } + casgstatus(gp, _Gwaiting, _Grunnable) + if !wasmSched.runq.Push(gp) { + fatal("runtime: invalid run queue insertion") + } +} + +func resumeWasmG(old, next *g) { + if old == nil || next == nil || next.context == nil { + fatal("runtime: invalid WebAssembly context switch") + return + } + ensureCurrentWasmFiber(old) + if next.context.platform.asyncifyStack == nil { + fatal("runtime: uninitialized WebAssembly goroutine context") + return + } + + casgstatus(next, _Grunnable, _Grunning) + mp := &wasmSched.m + old.m = nil + next.m = mp + mp.curg = next + setg(next) + old.context.platform.fiber.Swap(&next.context.platform.fiber) + reapRetiredWasmG() +} + +func goexitBackend(gp *g) { + releaseGAndCheckDeadlock() + next := popWasmRunq() + if next == nil { + fatal("no goroutines (main called runtime.Goexit) - deadlock!") + return + } + + casgstatus(gp, _Grunning, _Gdead) + if wasmSched.retired != nil { + fatal("runtime: unreaped WebAssembly goroutine") + return + } + wasmSched.retired = gp.context + resumeDeadWasmG(gp, next) +} + +func resumeDeadWasmG(old, next *g) { + ensureCurrentWasmFiber(old) + casgstatus(next, _Grunnable, _Grunning) + mp := &wasmSched.m + old.m = nil + next.m = mp + mp.curg = next + setg(next) + old.context.platform.fiber.Swap(&next.context.platform.fiber) + fatal("runtime: resumed dead WebAssembly goroutine") +} + +func reapRetiredWasmG() { + ctx := wasmSched.retired + if ctx == nil { + return + } + wasmSched.retired = nil + platform := &ctx.platform + if platform.stack != nil { + FreeRoot(platform.stack) + platform.stack = nil + } + if platform.asyncifyStack != nil { + FreeRoot(platform.asyncifyStack) + platform.asyncifyStack = nil + } + freeRuntimeContext(ctx) +} + +func popWasmRunq() *g { + return wasmSched.runq.Pop() +} + +// CurrentGForTesting returns an opaque handle suitable for ReadyForTesting. +func CurrentGForTesting() unsafe.Pointer { + return unsafe.Pointer(getg()) +} + +// ParkForTesting parks the current G until another G marks it runnable. +func ParkForTesting() { + gopark() +} + +// ReadyForTesting makes a G previously parked by ParkForTesting runnable. +func ReadyForTesting(handle unsafe.Pointer) { + goready((*g)(handle)) +} + +// SchedulerStateForTesting reports single-worker queue and ownership state. +func SchedulerStateForTesting() (runq uintptr, mid int64, pid int32) { + return wasmSched.runq.Len(), wasmSched.m.id, wasmSched.p.id +} + +func GMPForTesting() (goid, parentGoid uint64, mid int64, pid int32, gstatus, pstatus uint32, linked bool) { + gp := getg() + if gp == nil || gp.m == nil || gp.m.p == nil { + return + } + mp := gp.m + pp := mp.p + ctx := gp.context + return gp.goid, gp.parentGoid, mp.id, pp.id, readgstatus(gp), readpstatus(pp), + mp == &wasmSched.m && pp == &wasmSched.p && + mp.curg == gp && pp.m == mp && ctx != nil && &ctx.g == gp +} diff --git a/runtime/internal/runtime/runtime2.go b/runtime/internal/runtime/runtime2.go index 7787c8e225..fc3fa7b345 100644 --- a/runtime/internal/runtime/runtime2.go +++ b/runtime/internal/runtime/runtime2.go @@ -23,6 +23,7 @@ import "unsafe" const ( _Grunnable = 1 _Grunning = 2 + _Gwaiting = 4 _Gdead = 6 ) @@ -55,11 +56,32 @@ type g struct { goexit bool isMain bool paniconfault bool + + runqQueued uint32 + runqNext *g +} + +func (gp *g) RunqueueNext() *g { + return gp.runqNext +} + +func (gp *g) SetRunqueueNext(next *g) { + gp.runqNext = next +} + +func (gp *g) RunqueueQueued() bool { + return gp.runqQueued != 0 +} + +func (gp *g) SetRunqueueQueued(queued bool) { + if queued { + gp.runqQueued = 1 + } else { + gp.runqQueued = 0 + } } -// m represents the host execution resource running Go code. The platform -// thread handle is deliberately confined to mOS so other backends do not leak -// pthread types into the scheduler core. +// m represents the host execution resource running Go code. type m struct { curg *g p *p @@ -67,9 +89,7 @@ type m struct { os mOS } -// p represents the scheduling resources attached to an M. The pthread backend -// currently binds one P to one M; a later M:N scheduler can retain this object -// while replacing that fixed binding with a P pool and run queues. +// p represents the scheduling resources attached to an M. type p struct { id int32 status uint32 diff --git a/runtime/internal/runtime/z_default.go b/runtime/internal/runtime/z_default.go index e73aeff206..b322ae4d0e 100644 --- a/runtime/internal/runtime/z_default.go +++ b/runtime/internal/runtime/z_default.go @@ -28,11 +28,8 @@ func Rethrow(link *Defer) { c.Siglongjmp(link.Addr, 1) } } else if gp.goexit { - // Goexit must run deferred functions before terminating the current - // goroutine. Reuse the longjmp-based defer unwinding: - // 1) If we have a defer frame, longjmp to it so it can execute defers. - // 2) Once we've unwound past the last frame (link==nil), terminate the - // current pthread. + // Goexit runs deferred functions before the selected scheduler removes + // the current goroutine. if link != nil { c.Siglongjmp(link.Addr, 1) } @@ -40,6 +37,6 @@ func Rethrow(link *Defer) { markMainExited() } leaveCurrentLocalContext() - exitCurrentM() + goexitBackend(gp) } } From b496e3a2947d7d3913112b6293bb96ebb32e15e6 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Mon, 27 Jul 2026 12:42:53 +0800 Subject: [PATCH 02/18] test(runtime): exercise wasm scheduler in Node --- .github/workflows/llgo.yml | 2 + internal/build/build_test.go | 2 +- internal/build/outputs_test.go | 23 +++ .../build/testdata/wasm-scheduler/main.go | 133 ++++++++++++++++++ internal/crosscompile/crosscompile_test.go | 10 ++ .../internal/clite/emscripten/fiber_test.go | 12 ++ runtime/internal/runqueue/runqueue_test.go | 60 ++++++++ 7 files changed, 241 insertions(+), 1 deletion(-) create mode 100644 internal/build/testdata/wasm-scheduler/main.go create mode 100644 runtime/internal/clite/emscripten/fiber_test.go create mode 100644 runtime/internal/runqueue/runqueue_test.go diff --git a/.github/workflows/llgo.yml b/.github/workflows/llgo.yml index 40eff89107..262d958d92 100644 --- a/.github/workflows/llgo.yml +++ b/.github/workflows/llgo.yml @@ -392,4 +392,6 @@ jobs: run: | GOOS=js GOARCH=wasm llgo build -o "$RUNNER_TEMP/runtime-js.wasm" ./internal/build/testdata/wasm-runtime GOOS=wasip1 GOARCH=wasm llgo build -o "$RUNNER_TEMP/runtime-wasip1.wasm" ./internal/build/testdata/wasm-runtime + GOOS=js GOARCH=wasm llgo build -target wasm -o "$RUNNER_TEMP/wasm-scheduler.mjs" ./internal/build/testdata/wasm-scheduler + node --input-type=module -e "import Module from '$RUNNER_TEMP/wasm-scheduler.mjs'; await Module();" file "$RUNNER_TEMP/runtime-js.wasm" "$RUNNER_TEMP/runtime-wasip1.wasm" diff --git a/internal/build/build_test.go b/internal/build/build_test.go index 74951d5cd1..33c3b1afda 100644 --- a/internal/build/build_test.go +++ b/internal/build/build_test.go @@ -332,7 +332,7 @@ func TestDefaultBuildTags(t *testing.T) { }{ {name: "native", goarch: "arm64", want: base}, {name: "raw wasm", goarch: "wasm", want: base + ",nogc"}, - {name: "configured wasm target", goarch: "wasm", target: "wasip1", want: base}, + {name: "configured wasm target", goarch: "wasm", target: "wasip1", want: base + ",nogc"}, } { t.Run(test.name, func(t *testing.T) { if got := defaultBuildTags(test.goarch, test.target); got != test.want { diff --git a/internal/build/outputs_test.go b/internal/build/outputs_test.go index db2667b290..61c17e1c58 100644 --- a/internal/build/outputs_test.go +++ b/internal/build/outputs_test.go @@ -185,6 +185,29 @@ func TestBuildOutFmtsWithTarget(t *testing.T) { } } +func TestDefaultAppExtJSExplicitGlueOutput(t *testing.T) { + tests := []struct { + out string + want string + }{ + {out: "app.mjs", want: ".mjs"}, + {out: "app.js", want: ".js"}, + {out: "app.wasm", want: ".wasm"}, + {want: ".wasm"}, + } + for _, tt := range tests { + conf := &Config{ + Goos: "js", + Goarch: "wasm", + BuildMode: BuildModeExe, + OutFile: tt.out, + } + if got := defaultAppExt(conf); got != tt.want { + t.Errorf("defaultAppExt(%q) = %q, want %q", tt.out, got, tt.want) + } + } +} + func TestBuildOutFmtsNativeTarget(t *testing.T) { tests := []struct { name string diff --git a/internal/build/testdata/wasm-scheduler/main.go b/internal/build/testdata/wasm-scheduler/main.go new file mode 100644 index 0000000000..8e3d06880c --- /dev/null +++ b/internal/build/testdata/wasm-scheduler/main.go @@ -0,0 +1,133 @@ +package main + +import ( + "runtime" + "unsafe" +) + +//go:linkname currentGForTesting github.com/goplus/llgo/runtime/internal/runtime.CurrentGForTesting +func currentGForTesting() unsafe.Pointer + +//go:linkname parkForTesting github.com/goplus/llgo/runtime/internal/runtime.ParkForTesting +func parkForTesting() + +//go:linkname readyForTesting github.com/goplus/llgo/runtime/internal/runtime.ReadyForTesting +func readyForTesting(unsafe.Pointer) + +//go:linkname schedulerStateForTesting github.com/goplus/llgo/runtime/internal/runtime.SchedulerStateForTesting +func schedulerStateForTesting() (runq uintptr, mid int64, pid int32) + +//go:linkname gmpForTesting github.com/goplus/llgo/runtime/internal/runtime.GMPForTesting +func gmpForTesting() (goid, parentGoid uint64, mid int64, pid int32, gstatus, pstatus uint32, linked bool) + +var ( + parked unsafe.Pointer + mainMID int64 + mainPID int32 + mainGID uint64 + seenG [4]uint64 + seenGCount int + eventLog [8]int + eventCount int + done int +) + +func event(value int) { + eventLog[eventCount] = value + eventCount++ +} + +func checkCurrentG() { + goid, parent, mid, pid, gstatus, pstatus, linked := gmpForTesting() + if goid == 0 || goid == mainGID || parent != mainGID { + panic("invalid goroutine identity") + } + if mid != mainMID || pid != mainPID { + panic("goroutine did not reuse the single worker M/P") + } + if gstatus != 2 || pstatus != 1 || !linked { + panic("invalid running G/M/P state") + } + for i := 0; i < seenGCount; i++ { + if seenG[i] == goid { + panic("duplicate goroutine identity") + } + } + seenG[seenGCount] = goid + seenGCount++ +} + +func main() { + var ( + gstatus uint32 + pstatus uint32 + linked bool + ) + mainGID, _, mainMID, mainPID, gstatus, pstatus, linked = gmpForTesting() + if mainGID == 0 || mainMID == 0 || mainPID < 0 || gstatus != 2 || pstatus != 1 || !linked { + panic("invalid main G/M/P state") + } + + go func() { + checkCurrentG() + event(1) + parked = currentGForTesting() + parkForTesting() + event(8) + done++ + }() + + go func() { + checkCurrentG() + event(2) + runtime.Gosched() + event(6) + readyForTesting(parked) + event(7) + done++ + }() + + go func() { + checkCurrentG() + defer func() { + if recover() != "expected panic" { + panic("unexpected recover value") + } + event(3) + done++ + }() + panic("expected panic") + }() + + go func() { + checkCurrentG() + defer func() { + event(4) + done++ + }() + runtime.Goexit() + panic("Goexit returned") + }() + + if runq, mid, pid := schedulerStateForTesting(); runq != 4 || mid != mainMID || pid != mainPID { + panic("invalid initial scheduler state") + } + event(0) + for done != 4 { + runtime.Gosched() + } + + want := [...]int{0, 1, 2, 3, 4, 6, 7, 8} + if eventCount != len(want) { + panic("unexpected event count") + } + for i, value := range want { + if eventLog[i] != value { + panic("unexpected scheduler order") + } + } + if seenGCount != len(seenG) { + panic("not all goroutines ran") + } + println("wasm scheduler ok") +} diff --git a/internal/crosscompile/crosscompile_test.go b/internal/crosscompile/crosscompile_test.go index ea89a9596a..56edf0e498 100644 --- a/internal/crosscompile/crosscompile_test.go +++ b/internal/crosscompile/crosscompile_test.go @@ -172,6 +172,16 @@ func TestUseCrossCompileSDK(t *testing.T) { } } +func TestUseJSSupportsNode(t *testing.T) { + export, err := use("js", "wasm", false, false, optlevel.Oz, lto.Off, false) + if err != nil { + t.Fatal(err) + } + if !slices.Contains(export.LDFLAGS, "-sENVIRONMENT=web,worker,node") { + t.Fatalf("LDFLAGS do not enable Node: %v", export.LDFLAGS) + } +} + func TestUseTarget(t *testing.T) { // Test cases for target-based configuration testCases := []struct { diff --git a/runtime/internal/clite/emscripten/fiber_test.go b/runtime/internal/clite/emscripten/fiber_test.go new file mode 100644 index 0000000000..da2456dbc1 --- /dev/null +++ b/runtime/internal/clite/emscripten/fiber_test.go @@ -0,0 +1,12 @@ +package emscripten + +import ( + "testing" + "unsafe" +) + +func TestFiberStorageUsesEightWords(t *testing.T) { + if got, want := unsafe.Sizeof(Fiber{}), uintptr(8)*unsafe.Sizeof(uintptr(0)); got != want { + t.Fatalf("Fiber size = %d, want %d", got, want) + } +} diff --git a/runtime/internal/runqueue/runqueue_test.go b/runtime/internal/runqueue/runqueue_test.go new file mode 100644 index 0000000000..38a9fd5fb7 --- /dev/null +++ b/runtime/internal/runqueue/runqueue_test.go @@ -0,0 +1,60 @@ +package runqueue + +import "testing" + +type testNode struct { + value int + queued bool + next *testNode +} + +func (node *testNode) RunqueueNext() *testNode { + return node.next +} + +func (node *testNode) SetRunqueueNext(next *testNode) { + node.next = next +} + +func (node *testNode) RunqueueQueued() bool { + return node.queued +} + +func (node *testNode) SetRunqueueQueued(queued bool) { + node.queued = queued +} + +func TestQueueFIFOAndReuse(t *testing.T) { + first := &testNode{value: 1} + second := &testNode{value: 2} + var q Queue[*testNode] + + if !q.Push(first) || !q.Push(second) { + t.Fatal("Push rejected initialized nodes") + } + if q.Push(first) { + t.Fatal("Push accepted a queued node") + } + if got := q.Len(); got != 2 { + t.Fatalf("Len = %d, want 2", got) + } + if got := q.Pop(); got != first || got.value != 1 { + t.Fatalf("first Pop = %p, want %p", got, first) + } + if got := q.Pop(); got != second || got.value != 2 { + t.Fatalf("second Pop = %p, want %p", got, second) + } + if got := q.Pop(); got != nil { + t.Fatalf("empty Pop = %p, want nil", got) + } + if !q.Push(first) || q.Pop() != first { + t.Fatal("queue did not accept a reused node") + } +} + +func TestQueueRejectsInvalidNodes(t *testing.T) { + var q Queue[*testNode] + if q.Push(nil) { + t.Fatal("Push accepted nil") + } +} From 35fc518de76d035bdb68ac387caac9751a58319d Mon Sep 17 00:00:00 2001 From: Li Jie Date: Mon, 27 Jul 2026 13:10:27 +0800 Subject: [PATCH 03/18] build/wasm: distinguish Go Memory64 from wasm32 target --- .github/workflows/llgo.yml | 9 ++++- .github/workflows/targets.yml | 5 +++ internal/build/build.go | 20 ++++++---- internal/build/build_test.go | 24 ++++++++++++ internal/build/source_patch_test.go | 37 ++++++++++++------- internal/build/testdata/wasm-scheduler/abi.c | 5 +++ internal/build/testdata/wasm-scheduler/abi.go | 8 ++++ .../build/testdata/wasm-scheduler/main.go | 1 + .../build/testdata/wasm-scheduler/model_go.go | 14 +++++++ .../testdata/wasm-scheduler/model_target.go | 14 +++++++ internal/crosscompile/crosscompile.go | 30 ++++++++++++++- internal/crosscompile/crosscompile_test.go | 28 ++++++++++++++ runtime/internal/clite/c.go | 2 +- .../internal/clite/ctypes_selection_test.go | 34 +++++++++++++++++ runtime/internal/clite/ctypes_wasm.go | 5 +-- runtime/internal/clite/ctypes_wasm64.go | 25 +++++++++++++ runtime/internal/clite/emscripten/fiber.go | 2 +- ssa/ssa_test.go | 18 +++++++++ ssa/target.go | 5 +++ 19 files changed, 259 insertions(+), 27 deletions(-) create mode 100644 internal/build/testdata/wasm-scheduler/abi.c create mode 100644 internal/build/testdata/wasm-scheduler/abi.go create mode 100644 internal/build/testdata/wasm-scheduler/model_go.go create mode 100644 internal/build/testdata/wasm-scheduler/model_target.go create mode 100644 runtime/internal/clite/ctypes_selection_test.go create mode 100644 runtime/internal/clite/ctypes_wasm64.go diff --git a/.github/workflows/llgo.yml b/.github/workflows/llgo.yml index 262d958d92..5fb8bb0201 100644 --- a/.github/workflows/llgo.yml +++ b/.github/workflows/llgo.yml @@ -374,6 +374,11 @@ jobs: with: version: "4.0.21" + - name: Set up Node.js + uses: actions/setup-node@v7 + with: + node-version: "25" + - name: Set up Go for building llgo uses: ./.github/actions/setup-go @@ -392,6 +397,8 @@ jobs: run: | GOOS=js GOARCH=wasm llgo build -o "$RUNNER_TEMP/runtime-js.wasm" ./internal/build/testdata/wasm-runtime GOOS=wasip1 GOARCH=wasm llgo build -o "$RUNNER_TEMP/runtime-wasip1.wasm" ./internal/build/testdata/wasm-runtime - GOOS=js GOARCH=wasm llgo build -target wasm -o "$RUNNER_TEMP/wasm-scheduler.mjs" ./internal/build/testdata/wasm-scheduler + GOOS=js GOARCH=wasm llgo build -o "$RUNNER_TEMP/wasm-scheduler-go.mjs" ./internal/build/testdata/wasm-scheduler + node --input-type=module -e "import Module from '$RUNNER_TEMP/wasm-scheduler-go.mjs'; await Module();" + llgo build -target wasm -o "$RUNNER_TEMP/wasm-scheduler.mjs" ./internal/build/testdata/wasm-scheduler node --input-type=module -e "import Module from '$RUNNER_TEMP/wasm-scheduler.mjs'; await Module();" file "$RUNNER_TEMP/runtime-js.wasm" "$RUNNER_TEMP/runtime-wasip1.wasm" diff --git a/.github/workflows/targets.yml b/.github/workflows/targets.yml index 49ce34aa6c..d08d96bd7d 100644 --- a/.github/workflows/targets.yml +++ b/.github/workflows/targets.yml @@ -27,6 +27,11 @@ jobs: with: llvm-version: ${{matrix.llvm}} + - name: Set up Emscripten + uses: emscripten-core/setup-emsdk@v15 + with: + version: "4.0.21" + - name: Set up Go for build uses: ./.github/actions/setup-go diff --git a/internal/build/build.go b/internal/build/build.go index 81cbb024ea..65f742d52c 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -251,9 +251,6 @@ func resolveBuildConfig(input *Config) (*Config, error) { if conf.Goarch == "" { conf.Goarch = runtime.GOARCH } - if conf.AppExt == "" { - conf.AppExt = defaultAppExt(conf) - } if conf.BuildMode == "" { conf.BuildMode = BuildModeExe } @@ -399,6 +396,9 @@ func Build(inv Invocation) ([]Package, error) { if conf.Target != "" && export.GOARCH != "" { conf.Goarch = export.GOARCH } + if conf.AppExt == "" { + conf.AppExt = defaultAppExt(conf) + } if err := validateLinkOptions(conf, &export); err != nil { return nil, err } @@ -479,10 +479,7 @@ func Build(inv Invocation) ([]Package, error) { // final-PC sites for sidecar construction. prog.EnableFuncInfoSites(shouldEnablePCLNSites(conf, funcInfo, emitDebugInfo)) sizes := func(sizes types.Sizes, compiler, arch string) types.Sizes { - if arch == "wasm" { - sizes = &types.StdSizes{WordSize: 4, MaxAlign: 4} - } - return prog.TypeSizes(sizes) + return prog.TypeSizes(effectiveTypeSizes(sizes, conf.Goos, arch, conf.Target)) } dedup := packages.NewDeduper() var syntaxErr error @@ -800,6 +797,15 @@ func defaultBuildTags(goarch, target string) string { return tags } +func effectiveTypeSizes(sizes types.Sizes, goos, goarch, target string) types.Sizes { + // Named wasm targets use the native wasm32 data model. The raw js/wasm + // entry point keeps Go's 64-bit word model and is emitted as Memory64. + if goarch == "wasm" && (target != "" || goos != "js") { + return &types.StdSizes{WordSize: 4, MaxAlign: 4} + } + return sizes +} + func allowMissingFunctionBodies(initial []*packages.Package) { for _, pkg := range initial { hasMissingBody := false diff --git a/internal/build/build_test.go b/internal/build/build_test.go index 33c3b1afda..fcdbbabd2b 100644 --- a/internal/build/build_test.go +++ b/internal/build/build_test.go @@ -342,6 +342,30 @@ func TestDefaultBuildTags(t *testing.T) { } } +func TestEffectiveWasmTypeSizes(t *testing.T) { + goSizes := types.SizesFor("gc", "wasm") + for _, test := range []struct { + name string + goos string + target string + want int64 + }{ + {name: "Go js wasm", goos: "js", want: 8}, + {name: "configured wasm", goos: "js", target: "wasm", want: 4}, + {name: "WASI compatibility", goos: "wasip1", want: 4}, + } { + t.Run(test.name, func(t *testing.T) { + got := effectiveTypeSizes(goSizes, test.goos, "wasm", test.target) + if size := got.Sizeof(types.Typ[types.Uintptr]); size != test.want { + t.Fatalf("uintptr size = %d, want %d", size, test.want) + } + }) + } + if got := effectiveTypeSizes(goSizes, "linux", "amd64", ""); got != goSizes { + t.Fatal("native type sizes changed") + } +} + func TestWasmRuntimeAvoidsNativeHostDependencies(t *testing.T) { runtimeDir := filepath.Join(env.LLGoRuntimeDir(), "internal", "lib", "runtime") for _, goos := range []string{"js", "wasip1"} { diff --git a/internal/build/source_patch_test.go b/internal/build/source_patch_test.go index a4654cc752..84d214f759 100644 --- a/internal/build/source_patch_test.go +++ b/internal/build/source_patch_test.go @@ -19,29 +19,40 @@ import ( ) func TestWasmRuntimeSourcePatchTypeChecks(t *testing.T) { - for _, goos := range []string{"js", "wasip1"} { - t.Run(goos, func(t *testing.T) { - cfgEnv := append(os.Environ(), "GOOS="+goos, "GOARCH=wasm") + for _, test := range []struct { + name string + goos string + target string + buildFlags []string + }{ + {name: "js Memory64", goos: "js"}, + {name: "js wasm32 target", goos: "js", target: "wasm", buildFlags: []string{"-tags=tinygo.wasm"}}, + {name: "WASI wasm32", goos: "wasip1"}, + } { + t.Run(test.name, func(t *testing.T) { + cfgEnv := append(os.Environ(), "GOOS="+test.goos, "GOARCH=wasm") goroot, goversion, err := env.GOROOTAndGOVERSIONWithEnv(cfgEnv) if err != nil { t.Fatal(err) } overlay, _, err := buildSourcePatchOverlayForGOROOT(nil, env.LLGoRuntimeDir(), goroot, sourcePatchBuildContext{ - goos: goos, - goarch: "wasm", - goversion: goversion, + goos: test.goos, + goarch: "wasm", + goversion: goversion, + buildFlags: test.buildFlags, }) if err != nil { t.Fatal(err) } - pkgs, err := packages.LoadEx(nil, func(types.Sizes, string, string) types.Sizes { - return &types.StdSizes{WordSize: 4, MaxAlign: 4} + pkgs, err := packages.LoadEx(nil, func(sizes types.Sizes, _ string, arch string) types.Sizes { + return effectiveTypeSizes(sizes, test.goos, arch, test.target) }, &packages.Config{ - Mode: loadSyntax | packages.NeedDeps | packages.NeedModule | packages.NeedExportFile, - Env: cfgEnv, - Fset: token.NewFileSet(), - Overlay: overlay, + Mode: loadSyntax | packages.NeedDeps | packages.NeedModule | packages.NeedExportFile, + Env: cfgEnv, + Fset: token.NewFileSet(), + Overlay: overlay, + BuildFlags: test.buildFlags, }, "runtime") if err != nil { t.Fatal(err) @@ -51,7 +62,7 @@ func TestWasmRuntimeSourcePatchTypeChecks(t *testing.T) { } if pkgs[0].IllTyped { logPackageErrors(t, pkgs[0], make(map[string]bool)) - t.Fatal("runtime did not type-check with wasm32 sizes") + t.Fatal("runtime did not type-check") } }) } diff --git a/internal/build/testdata/wasm-scheduler/abi.c b/internal/build/testdata/wasm-scheduler/abi.c new file mode 100644 index 0000000000..2f6f6ff3ba --- /dev/null +++ b/internal/build/testdata/wasm-scheduler/abi.c @@ -0,0 +1,5 @@ +#include + +size_t llgo_test_sizeof_long(void) { + return sizeof(long); +} diff --git a/internal/build/testdata/wasm-scheduler/abi.go b/internal/build/testdata/wasm-scheduler/abi.go new file mode 100644 index 0000000000..3004ad69d4 --- /dev/null +++ b/internal/build/testdata/wasm-scheduler/abi.go @@ -0,0 +1,8 @@ +package main + +import _ "unsafe" + +const LLGoFiles = "abi.c" + +//go:linkname cLongSize C.llgo_test_sizeof_long +func cLongSize() uintptr diff --git a/internal/build/testdata/wasm-scheduler/main.go b/internal/build/testdata/wasm-scheduler/main.go index 8e3d06880c..b2b1900d7a 100644 --- a/internal/build/testdata/wasm-scheduler/main.go +++ b/internal/build/testdata/wasm-scheduler/main.go @@ -58,6 +58,7 @@ func checkCurrentG() { } func main() { + checkWasmModel() var ( gstatus uint32 pstatus uint32 diff --git a/internal/build/testdata/wasm-scheduler/model_go.go b/internal/build/testdata/wasm-scheduler/model_go.go new file mode 100644 index 0000000000..73781131c4 --- /dev/null +++ b/internal/build/testdata/wasm-scheduler/model_go.go @@ -0,0 +1,14 @@ +//go:build !tinygo.wasm + +package main + +import "unsafe" + +func checkWasmModel() { + if unsafe.Sizeof(uintptr(0)) != 8 { + panic("GOOS/GOARCH wasm must use 64-bit words") + } + if cLongSize() != 8 { + panic("GOOS/GOARCH wasm must use the LP64 C data model") + } +} diff --git a/internal/build/testdata/wasm-scheduler/model_target.go b/internal/build/testdata/wasm-scheduler/model_target.go new file mode 100644 index 0000000000..1fadc4efc1 --- /dev/null +++ b/internal/build/testdata/wasm-scheduler/model_target.go @@ -0,0 +1,14 @@ +//go:build tinygo.wasm + +package main + +import "unsafe" + +func checkWasmModel() { + if unsafe.Sizeof(uintptr(0)) != 4 { + panic("-target wasm must use 32-bit words") + } + if cLongSize() != 4 { + panic("-target wasm must use the wasm32 C data model") + } +} diff --git a/internal/crosscompile/crosscompile.go b/internal/crosscompile/crosscompile.go index 614ac0eeae..8e2c6943c0 100644 --- a/internal/crosscompile/crosscompile.go +++ b/internal/crosscompile/crosscompile.go @@ -218,6 +218,10 @@ func compileWithConfig( } func use(goos, goarch string, wasiThreads, forceEspClang bool, level optlevel.Level, ltoMode lto.Mode, goGlobalDCE bool) (export Export, err error) { + return useWithJSWasm32(goos, goarch, wasiThreads, forceEspClang, level, ltoMode, goGlobalDCE, false) +} + +func useWithJSWasm32(goos, goarch string, wasiThreads, forceEspClang bool, level optlevel.Level, ltoMode lto.Mode, goGlobalDCE, jsWasm32 bool) (export Export, err error) { targetTriple := llvm.GetTargetTriple(goos, goarch) llgoRoot := env.LLGoROOT() @@ -402,6 +406,7 @@ func use(goos, goarch string, wasiThreads, forceEspClang bool, level optlevel.Le "-fwasm-exceptions", "-mllvm", "-wasm-enable-sjlj", }...) + export.LLVMTarget = "wasm32-unknown-wasip1" // Add thread support if enabled if wasiThreads { export.CCFLAGS = append( @@ -417,7 +422,13 @@ func use(goos, goarch string, wasiThreads, forceEspClang bool, level optlevel.Le } case "js": - targetTriple := "wasm32-unknown-emscripten" + // The Go wasm type model uses 64-bit words. Use Memory64 so LLVM + // pointers have the same width; named wasm targets retain wasm32. + targetTriple := "wasm64-unknown-emscripten" + if jsWasm32 { + targetTriple = "wasm32-unknown-emscripten" + } + export.LLVMTarget = targetTriple // Emscripten configuration using system installation // Specify emcc as the compiler export.CC = "emcc" @@ -457,6 +468,9 @@ func use(goos, goarch string, wasiThreads, forceEspClang bool, level optlevel.Le "-sASYNCIFY=1", "-sSTACK_SIZE=5242880", // 50MB }...) + if !jsWasm32 { + export.LDFLAGS = append(export.LDFLAGS, "-sMEMORY64=1") + } default: err = errors.New("unsupported GOOS for WebAssembly: " + goos) @@ -721,5 +735,19 @@ func Use(goos, goarch, targetName string, wasiThreads, forceEspClang bool, level if targetName != "" && !strings.HasPrefix(targetName, "wasm") && !strings.HasPrefix(targetName, "wasi") { return UseTarget(targetName, level, ltoMode) } + if targetName == "wasm" { + config, err := targets.NewDefaultResolver().Resolve(targetName) + if err != nil { + return export, err + } + export, err = useWithJSWasm32(config.GOOS, config.GOARCH, wasiThreads, forceEspClang, level, ltoMode, goGlobalDCE, true) + if err != nil { + return export, err + } + export.BuildTags = config.BuildTags + export.GOOS = config.GOOS + export.GOARCH = config.GOARCH + return export, nil + } return use(goos, goarch, wasiThreads, forceEspClang, level, ltoMode, goGlobalDCE) } diff --git a/internal/crosscompile/crosscompile_test.go b/internal/crosscompile/crosscompile_test.go index 56edf0e498..41f933f940 100644 --- a/internal/crosscompile/crosscompile_test.go +++ b/internal/crosscompile/crosscompile_test.go @@ -177,9 +177,37 @@ func TestUseJSSupportsNode(t *testing.T) { if err != nil { t.Fatal(err) } + if export.LLVMTarget != "wasm64-unknown-emscripten" { + t.Fatalf("LLVMTarget = %q, want wasm64-unknown-emscripten", export.LLVMTarget) + } if !slices.Contains(export.LDFLAGS, "-sENVIRONMENT=web,worker,node") { t.Fatalf("LDFLAGS do not enable Node: %v", export.LDFLAGS) } + if !slices.Contains(export.LDFLAGS, "-sMEMORY64=1") { + t.Fatalf("LDFLAGS do not enable Memory64: %v", export.LDFLAGS) + } +} + +func TestUseWasmTargetSelectsGoPlatform(t *testing.T) { + export, err := Use(runtime.GOOS, runtime.GOARCH, "wasm", false, false, optlevel.Oz, lto.Off, false) + if err != nil { + t.Fatal(err) + } + if export.GOOS != "js" || export.GOARCH != "wasm" { + t.Fatalf("GOOS/GOARCH = %s/%s, want js/wasm", export.GOOS, export.GOARCH) + } + if export.CC != "emcc" { + t.Fatalf("CC = %q, want emcc", export.CC) + } + if export.LLVMTarget != "wasm32-unknown-emscripten" { + t.Fatalf("LLVMTarget = %q, want wasm32-unknown-emscripten", export.LLVMTarget) + } + if !slices.Contains(export.BuildTags, "tinygo.wasm") { + t.Fatalf("BuildTags do not identify the wasm32 target: %v", export.BuildTags) + } + if slices.Contains(export.LDFLAGS, "-sMEMORY64=1") { + t.Fatalf("wasm32 LDFLAGS enable Memory64: %v", export.LDFLAGS) + } } func TestUseTarget(t *testing.T) { diff --git a/runtime/internal/clite/c.go b/runtime/internal/clite/c.go index 78a9897339..aa6f7a6fef 100644 --- a/runtime/internal/clite/c.go +++ b/runtime/internal/clite/c.go @@ -51,7 +51,7 @@ type integer interface { } type SizeT = uintptr -type SsizeT = Long +type SsizeT = int type IntptrT = uintptr type UintptrT = uintptr diff --git a/runtime/internal/clite/ctypes_selection_test.go b/runtime/internal/clite/ctypes_selection_test.go new file mode 100644 index 0000000000..1db4dc4c20 --- /dev/null +++ b/runtime/internal/clite/ctypes_selection_test.go @@ -0,0 +1,34 @@ +package c + +import ( + "go/build" + "slices" + "testing" +) + +func TestWasmCTypeFileSelection(t *testing.T) { + for _, test := range []struct { + name string + goos string + tags []string + want string + }{ + {name: "js Memory64", goos: "js", want: "ctypes_wasm64.go"}, + {name: "js wasm32 target", goos: "js", tags: []string{"tinygo.wasm"}, want: "ctypes_wasm.go"}, + {name: "WASI wasm32", goos: "wasip1", want: "ctypes_wasm.go"}, + } { + t.Run(test.name, func(t *testing.T) { + ctx := build.Default + ctx.GOOS = test.goos + ctx.GOARCH = "wasm" + ctx.BuildTags = test.tags + pkg, err := ctx.ImportDir(".", 0) + if err != nil { + t.Fatal(err) + } + if !slices.Contains(pkg.GoFiles, test.want) { + t.Fatalf("GoFiles = %v, want %s", pkg.GoFiles, test.want) + } + }) + } +} diff --git a/runtime/internal/clite/ctypes_wasm.go b/runtime/internal/clite/ctypes_wasm.go index 9b68f43a68..a580ccb6e5 100644 --- a/runtime/internal/clite/ctypes_wasm.go +++ b/runtime/internal/clite/ctypes_wasm.go @@ -1,5 +1,4 @@ -//go:build wasip1 || js -// +build wasip1 js +//go:build wasip1 || (js && tinygo.wasm) /* * Copyright (c) 2024 The XGo Authors (xgo.dev). All rights reserved. @@ -19,7 +18,7 @@ package c -// For WebAssembly targets, Long is 32-bit per the spec +// WASI and configured js/wasm targets use the wasm32 C data model. type ( Long = int32 Ulong = uint32 diff --git a/runtime/internal/clite/ctypes_wasm64.go b/runtime/internal/clite/ctypes_wasm64.go new file mode 100644 index 0000000000..1f6b409b79 --- /dev/null +++ b/runtime/internal/clite/ctypes_wasm64.go @@ -0,0 +1,25 @@ +//go:build js && !tinygo.wasm + +/* + * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package c + +// Emscripten Memory64 uses the LP64 C data model. +type ( + Long = int64 + Ulong = uint64 +) diff --git a/runtime/internal/clite/emscripten/fiber.go b/runtime/internal/clite/emscripten/fiber.go index 8490f8fd10..00a4fe87f7 100644 --- a/runtime/internal/clite/emscripten/fiber.go +++ b/runtime/internal/clite/emscripten/fiber.go @@ -21,7 +21,7 @@ package emscripten import c "github.com/goplus/llgo/runtime/internal/clite" // Fiber is the opaque emscripten_fiber_t storage. The C layout consists of -// eight pointer-sized fields on wasm32. +// eight pointer-sized fields. type Fiber struct { _ [8]uintptr } diff --git a/ssa/ssa_test.go b/ssa/ssa_test.go index 05f998fae0..31c9b567c3 100644 --- a/ssa/ssa_test.go +++ b/ssa/ssa_test.go @@ -2628,6 +2628,24 @@ func TestTargetMachineAndDataLayout(t *testing.T) { } } +func TestWasmTargetSpec(t *testing.T) { + for _, test := range []struct { + name string + target string + want string + }{ + {name: "Go environment", want: "wasm64-unknown-js"}, + {name: "configured target", target: "wasm", want: "wasm32-unknown-js"}, + } { + t.Run(test.name, func(t *testing.T) { + got := (&Target{GOOS: "js", GOARCH: "wasm", Target: test.target}).Spec().Triple + if got != test.want { + t.Fatalf("triple = %q, want %q", got, test.want) + } + }) + } +} + func TestAbiTables(t *testing.T) { prog := NewProgram(nil) prog.sizes = types.SizesFor("gc", runtime.GOARCH) diff --git a/ssa/target.go b/ssa/target.go index e9317f669c..c63a5855e0 100644 --- a/ssa/target.go +++ b/ssa/target.go @@ -147,6 +147,11 @@ func (p *Target) Spec() (spec TargetSpec) { } case "wasm": llvmarch = "wasm32" + // Keep raw js/wasm consistent with Go's 64-bit word model. Named + // targets use their existing wasm32 ABI. + if goos == "js" && p.Target == "" { + llvmarch = "wasm64" + } default: llvmarch = goarch } From 3ebba2aa5de8968674338ba18e8edf708c30d37e Mon Sep 17 00:00:00 2001 From: Li Jie Date: Mon, 27 Jul 2026 15:59:33 +0800 Subject: [PATCH 04/18] test(crosscompile): cover wasm target setup errors --- internal/crosscompile/crosscompile_test.go | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/internal/crosscompile/crosscompile_test.go b/internal/crosscompile/crosscompile_test.go index 41f933f940..66be579201 100644 --- a/internal/crosscompile/crosscompile_test.go +++ b/internal/crosscompile/crosscompile_test.go @@ -5,6 +5,7 @@ package crosscompile import ( "os" + "path/filepath" "runtime" "slices" "strings" @@ -210,6 +211,50 @@ func TestUseWasmTargetSelectsGoPlatform(t *testing.T) { } } +func TestUseWasmTargetErrors(t *testing.T) { + newLLGoRoot := func(t *testing.T, wasmConfig string) { + t.Helper() + root := t.TempDir() + runtimeDir := filepath.Join(root, "runtime") + if err := os.MkdirAll(runtimeDir, 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(runtimeDir, "go.mod"), []byte("module github.com/goplus/llgo/runtime\n"), 0o644); err != nil { + t.Fatal(err) + } + if wasmConfig != "" { + targetsDir := filepath.Join(root, "targets") + if err := os.MkdirAll(targetsDir, 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(targetsDir, "wasm.json"), []byte(wasmConfig), 0o644); err != nil { + t.Fatal(err) + } + } + t.Setenv("LLGO_ROOT", root) + } + + t.Run("resolve", func(t *testing.T) { + newLLGoRoot(t, "") + _, err := Use(runtime.GOOS, runtime.GOARCH, "wasm", false, false, optlevel.Oz, lto.Off, false) + if err == nil || !strings.Contains(err.Error(), "failed to resolve target wasm") { + t.Fatalf("Use error = %v, want target resolution error", err) + } + }) + + t.Run("toolchain setup", func(t *testing.T) { + newLLGoRoot(t, `{"goos":"js","goarch":"wasm"}`) + oldCacheRoot := cacheRoot + cacheRoot = func() string { return "\x00" } + defer func() { cacheRoot = oldCacheRoot }() + + _, err := Use(runtime.GOOS, runtime.GOARCH, "wasm", false, true, optlevel.Oz, lto.Off, false) + if err == nil { + t.Fatal("Use succeeded with an invalid toolchain cache path") + } + }) +} + func TestUseTarget(t *testing.T) { // Test cases for target-based configuration testCases := []struct { From a8c221d7788c4a226b1e910a52e74af1dd7c13e0 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Mon, 27 Jul 2026 23:57:30 +0800 Subject: [PATCH 05/18] runtime/wasm: make scheduler invariant failures fatal --- .github/workflows/llgo.yml | 15 +++++++++++++-- internal/build/testdata/wasm-scheduler/abi.c | 5 +++++ internal/build/testdata/wasm-scheduler/abi.go | 3 +++ internal/build/testdata/wasm-scheduler/main.go | 12 +++++++++++- runtime/internal/runtime/proc.go | 3 ++- runtime/internal/runtime/proc_atomic.go | 2 ++ runtime/internal/runtime/proc_wasm.go | 17 +++++++++++------ runtime/internal/runtime/runtime2.go | 8 ++++---- runtime/internal/runtime/stubs.go | 2 ++ 9 files changed, 53 insertions(+), 14 deletions(-) diff --git a/.github/workflows/llgo.yml b/.github/workflows/llgo.yml index 5fb8bb0201..af30af60ca 100644 --- a/.github/workflows/llgo.yml +++ b/.github/workflows/llgo.yml @@ -395,10 +395,21 @@ jobs: - name: Build standard runtime for wasm shell: bash run: | + run_wasm_scheduler() { + local module="$1" + local output + node --input-type=module -e "import Module from '$module'; await Module();" + if output=$(node --input-type=module -e "import Module from '$module'; await Module({preRun: [module => { module.ENV.LLGO_WASM_SCHEDULER_DEADLOCK = '1'; }]});" 2>&1); then + echo "deadlock scheduler fixture unexpectedly succeeded" + return 1 + fi + grep -Fq "fatal error: all goroutines are asleep - deadlock!" <<<"$output" + } + GOOS=js GOARCH=wasm llgo build -o "$RUNNER_TEMP/runtime-js.wasm" ./internal/build/testdata/wasm-runtime GOOS=wasip1 GOARCH=wasm llgo build -o "$RUNNER_TEMP/runtime-wasip1.wasm" ./internal/build/testdata/wasm-runtime GOOS=js GOARCH=wasm llgo build -o "$RUNNER_TEMP/wasm-scheduler-go.mjs" ./internal/build/testdata/wasm-scheduler - node --input-type=module -e "import Module from '$RUNNER_TEMP/wasm-scheduler-go.mjs'; await Module();" + run_wasm_scheduler "$RUNNER_TEMP/wasm-scheduler-go.mjs" llgo build -target wasm -o "$RUNNER_TEMP/wasm-scheduler.mjs" ./internal/build/testdata/wasm-scheduler - node --input-type=module -e "import Module from '$RUNNER_TEMP/wasm-scheduler.mjs'; await Module();" + run_wasm_scheduler "$RUNNER_TEMP/wasm-scheduler.mjs" file "$RUNNER_TEMP/runtime-js.wasm" "$RUNNER_TEMP/runtime-wasip1.wasm" diff --git a/internal/build/testdata/wasm-scheduler/abi.c b/internal/build/testdata/wasm-scheduler/abi.c index 2f6f6ff3ba..b648edf3ec 100644 --- a/internal/build/testdata/wasm-scheduler/abi.c +++ b/internal/build/testdata/wasm-scheduler/abi.c @@ -1,5 +1,10 @@ #include +#include size_t llgo_test_sizeof_long(void) { return sizeof(long); } + +int llgo_test_scheduler_deadlock(void) { + return getenv("LLGO_WASM_SCHEDULER_DEADLOCK") != NULL; +} diff --git a/internal/build/testdata/wasm-scheduler/abi.go b/internal/build/testdata/wasm-scheduler/abi.go index 3004ad69d4..caa04596fc 100644 --- a/internal/build/testdata/wasm-scheduler/abi.go +++ b/internal/build/testdata/wasm-scheduler/abi.go @@ -6,3 +6,6 @@ const LLGoFiles = "abi.c" //go:linkname cLongSize C.llgo_test_sizeof_long func cLongSize() uintptr + +//go:linkname schedulerDeadlockMode C.llgo_test_scheduler_deadlock +func schedulerDeadlockMode() int32 diff --git a/internal/build/testdata/wasm-scheduler/main.go b/internal/build/testdata/wasm-scheduler/main.go index b2b1900d7a..12fd8c81ae 100644 --- a/internal/build/testdata/wasm-scheduler/main.go +++ b/internal/build/testdata/wasm-scheduler/main.go @@ -59,13 +59,17 @@ func checkCurrentG() { func main() { checkWasmModel() + if schedulerDeadlockMode() != 0 { + testParkedMainDeadlock() + return + } var ( gstatus uint32 pstatus uint32 linked bool ) mainGID, _, mainMID, mainPID, gstatus, pstatus, linked = gmpForTesting() - if mainGID == 0 || mainMID == 0 || mainPID < 0 || gstatus != 2 || pstatus != 1 || !linked { + if mainGID != 1 || mainMID != 1 || mainPID != 0 || gstatus != 2 || pstatus != 1 || !linked { panic("invalid main G/M/P state") } @@ -132,3 +136,9 @@ func main() { } println("wasm scheduler ok") } + +func testParkedMainDeadlock() { + go func() {}() + parkForTesting() + panic("park returned after scheduler deadlock") +} diff --git a/runtime/internal/runtime/proc.go b/runtime/internal/runtime/proc.go index 1525f7fdcd..e200a1e5b9 100644 --- a/runtime/internal/runtime/proc.go +++ b/runtime/internal/runtime/proc.go @@ -118,7 +118,8 @@ func releaseGAndCheckDeadlock() { } } -// Gosched yields the processor, allowing another goroutine to run. +// Gosched asks the active backend to yield. The WebAssembly fiber backend +// switches to another runnable G; pthread Gs rely on the host thread scheduler. func Gosched() { goschedBackend() } diff --git a/runtime/internal/runtime/proc_atomic.go b/runtime/internal/runtime/proc_atomic.go index 1028c5a78b..c0652f926a 100644 --- a/runtime/internal/runtime/proc_atomic.go +++ b/runtime/internal/runtime/proc_atomic.go @@ -25,6 +25,8 @@ const ( gCountMask = mainExitedBit - 1 ) +// LLGo's atomic.Add returns the value before the addition. G and M reserve ID +// zero, while P IDs are zero-based like the Go runtime. func nextGoid(gp *g) uint64 { return atomic.Add(&sched.goidgen, uint64(1)) + 1 } diff --git a/runtime/internal/runtime/proc_wasm.go b/runtime/internal/runtime/proc_wasm.go index 184c00f7e6..7f698bcbfb 100644 --- a/runtime/internal/runtime/proc_wasm.go +++ b/runtime/internal/runtime/proc_wasm.go @@ -195,18 +195,23 @@ func resumeWasmG(old, next *g) { } func goexitBackend(gp *g) { + casgstatus(gp, _Grunning, _Gdead) + if wasmSched.retired != nil { + fatal("runtime: unreaped WebAssembly goroutine") + return + } releaseGAndCheckDeadlock() + next := popWasmRunq() if next == nil { - fatal("no goroutines (main called runtime.Goexit) - deadlock!") + if gp.isMain { + fatal("no goroutines (main called runtime.Goexit) - deadlock!") + } else { + fatal("all goroutines are asleep - deadlock!") + } return } - casgstatus(gp, _Grunning, _Gdead) - if wasmSched.retired != nil { - fatal("runtime: unreaped WebAssembly goroutine") - return - } wasmSched.retired = gp.context resumeDeadWasmG(gp, next) } diff --git a/runtime/internal/runtime/runtime2.go b/runtime/internal/runtime/runtime2.go index fc3fa7b345..a0efae65ec 100644 --- a/runtime/internal/runtime/runtime2.go +++ b/runtime/internal/runtime/runtime2.go @@ -19,7 +19,7 @@ package runtime import "unsafe" // These G and P states intentionally keep the values used by the Go runtime. -// Only states reachable by the current 1:1 backend are defined here. +// Only states reachable by the current backends are defined here. const ( _Grunnable = 1 _Grunning = 2 @@ -35,9 +35,9 @@ const ( // g holds state owned by one LLGo goroutine. // -// The current pthread backend gives every G its own M and P. Fields that only -// make sense once LLGo can suspend and resume a G (saved registers, wait state, -// and stack roots) belong here when those facilities are added. +// A backend decides the M/P ownership model: pthread gives every G its own M/P, +// while the WebAssembly fiber scheduler shares one M/P across its Gs. Suspended +// execution state is held by the backend-specific runtimeContext. type g struct { defer_ *Defer panic_ unsafe.Pointer diff --git a/runtime/internal/runtime/stubs.go b/runtime/internal/runtime/stubs.go index 61d9013b89..9c164d3ffb 100644 --- a/runtime/internal/runtime/stubs.go +++ b/runtime/internal/runtime/stubs.go @@ -7,6 +7,7 @@ package runtime import ( "unsafe" + c "github.com/goplus/llgo/runtime/internal/clite" "github.com/goplus/llgo/runtime/internal/clite/sync/atomic" "github.com/goplus/llgo/runtime/internal/clite/time" "github.com/goplus/llgo/runtime/internal/runtime/math" @@ -118,6 +119,7 @@ func memclrNoHeapPointers(ptr unsafe.Pointer, n uintptr) { func fatal(s string) { print("fatal error: ", s, "\n") + c.Exit(2) } func throw(s string) { From 01d386a330326b6d60797a3d7bbec4070b6c51c5 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Thu, 30 Jul 2026 06:59:04 +0800 Subject: [PATCH 06/18] runtime/wasm: isolate scheduler state from native Gs --- runtime/internal/runtime/fatal_default.go | 7 +++++++ runtime/internal/runtime/fatal_wasm.go | 10 ++++++++++ runtime/internal/runtime/proc_wasm.go | 18 ++++++++++++++++++ runtime/internal/runtime/runtime2.go | 23 ----------------------- runtime/internal/runtime/stubs.go | 6 ------ 5 files changed, 35 insertions(+), 29 deletions(-) create mode 100644 runtime/internal/runtime/fatal_default.go create mode 100644 runtime/internal/runtime/fatal_wasm.go diff --git a/runtime/internal/runtime/fatal_default.go b/runtime/internal/runtime/fatal_default.go new file mode 100644 index 0000000000..0046a9ec36 --- /dev/null +++ b/runtime/internal/runtime/fatal_default.go @@ -0,0 +1,7 @@ +//go:build !llgo || !js || !wasm + +package runtime + +func fatal(s string) { + print("fatal error: ", s, "\n") +} diff --git a/runtime/internal/runtime/fatal_wasm.go b/runtime/internal/runtime/fatal_wasm.go new file mode 100644 index 0000000000..3482947aab --- /dev/null +++ b/runtime/internal/runtime/fatal_wasm.go @@ -0,0 +1,10 @@ +//go:build llgo && js && wasm + +package runtime + +import c "github.com/goplus/llgo/runtime/internal/clite" + +func fatal(s string) { + print("fatal error: ", s, "\n") + c.Exit(2) +} diff --git a/runtime/internal/runtime/proc_wasm.go b/runtime/internal/runtime/proc_wasm.go index 7f698bcbfb..0550a6ff01 100644 --- a/runtime/internal/runtime/proc_wasm.go +++ b/runtime/internal/runtime/proc_wasm.go @@ -34,6 +34,8 @@ type runtimeContextPlatform struct { fiber emscripten.Fiber stack unsafe.Pointer asyncifyStack unsafe.Pointer + runqNext *g + runqQueued bool } var wasmSched struct { @@ -44,6 +46,22 @@ var wasmSched struct { started bool } +func (gp *g) RunqueueNext() *g { + return gp.context.platform.runqNext +} + +func (gp *g) SetRunqueueNext(next *g) { + gp.context.platform.runqNext = next +} + +func (gp *g) RunqueueQueued() bool { + return gp.context.platform.runqQueued +} + +func (gp *g) SetRunqueueQueued(queued bool) { + gp.context.platform.runqQueued = queued +} + func initRuntimeContext(ctx *runtimeContext, callergp *g, status uint32) *g { gp := initG(ctx, callergp, status) if status == _Grunning { diff --git a/runtime/internal/runtime/runtime2.go b/runtime/internal/runtime/runtime2.go index a0efae65ec..fc8a559221 100644 --- a/runtime/internal/runtime/runtime2.go +++ b/runtime/internal/runtime/runtime2.go @@ -56,29 +56,6 @@ type g struct { goexit bool isMain bool paniconfault bool - - runqQueued uint32 - runqNext *g -} - -func (gp *g) RunqueueNext() *g { - return gp.runqNext -} - -func (gp *g) SetRunqueueNext(next *g) { - gp.runqNext = next -} - -func (gp *g) RunqueueQueued() bool { - return gp.runqQueued != 0 -} - -func (gp *g) SetRunqueueQueued(queued bool) { - if queued { - gp.runqQueued = 1 - } else { - gp.runqQueued = 0 - } } // m represents the host execution resource running Go code. diff --git a/runtime/internal/runtime/stubs.go b/runtime/internal/runtime/stubs.go index 9c164d3ffb..6e50b2518d 100644 --- a/runtime/internal/runtime/stubs.go +++ b/runtime/internal/runtime/stubs.go @@ -7,7 +7,6 @@ package runtime import ( "unsafe" - c "github.com/goplus/llgo/runtime/internal/clite" "github.com/goplus/llgo/runtime/internal/clite/sync/atomic" "github.com/goplus/llgo/runtime/internal/clite/time" "github.com/goplus/llgo/runtime/internal/runtime/math" @@ -117,11 +116,6 @@ func memclrHasPointers(ptr unsafe.Pointer, n uintptr) { func memclrNoHeapPointers(ptr unsafe.Pointer, n uintptr) { } -func fatal(s string) { - print("fatal error: ", s, "\n") - c.Exit(2) -} - func throw(s string) { print("fatal error: ", s, "\n") } From 6d52762b1f79b4e2c4c972c7027622b0960f4275 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Mon, 3 Aug 2026 13:37:46 +0800 Subject: [PATCH 07/18] runtime/wasm: clean up failed fiber allocation --- runtime/internal/clite/c.go | 2 ++ runtime/internal/runtime/os_wasm.go | 4 ++-- runtime/internal/runtime/proc.go | 4 ++-- runtime/internal/runtime/proc_wasm.go | 22 ++++++++++++++++++---- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/runtime/internal/clite/c.go b/runtime/internal/clite/c.go index aa6f7a6fef..088991cb5b 100644 --- a/runtime/internal/clite/c.go +++ b/runtime/internal/clite/c.go @@ -51,6 +51,8 @@ type integer interface { } type SizeT = uintptr + +// ssize_t is the pointer-sized signed integer in every supported C data model. type SsizeT = int type IntptrT = uintptr diff --git a/runtime/internal/runtime/os_wasm.go b/runtime/internal/runtime/os_wasm.go index 956031b417..06cb527227 100644 --- a/runtime/internal/runtime/os_wasm.go +++ b/runtime/internal/runtime/os_wasm.go @@ -18,6 +18,6 @@ package runtime -// mOS is empty for the single-worker WebAssembly backend. The host Worker is -// owned by Emscripten rather than created for an individual M. +// mOS is empty for the single-worker WebAssembly backend. The host owns the +// physical worker instead of creating one for each M. type mOS struct{} diff --git a/runtime/internal/runtime/proc.go b/runtime/internal/runtime/proc.go index e200a1e5b9..57234487d2 100644 --- a/runtime/internal/runtime/proc.go +++ b/runtime/internal/runtime/proc.go @@ -118,8 +118,8 @@ func releaseGAndCheckDeadlock() { } } -// Gosched asks the active backend to yield. The WebAssembly fiber backend -// switches to another runnable G; pthread Gs rely on the host thread scheduler. +// Gosched asks the active backend to yield. WebAssembly backends re-queue the +// current G and yield to their scheduler; pthread Gs rely on the host scheduler. func Gosched() { goschedBackend() } diff --git a/runtime/internal/runtime/proc_wasm.go b/runtime/internal/runtime/proc_wasm.go index 0550a6ff01..f5ae5d71b5 100644 --- a/runtime/internal/runtime/proc_wasm.go +++ b/runtime/internal/runtime/proc_wasm.go @@ -89,13 +89,18 @@ func initWasmScheduler(gp *g) { func newprocBackend(fn goroutineFunc, arg unsafe.Pointer, stackSize uintptr, callergp *g) { gp := newproc1(fn, arg, callergp) - initWasmFiber(gp, stackSize) + if !initWasmFiber(gp, stackSize) { + releaseG() + FreeRoot(arg) + freeRuntimeContext(gp.context) + panic("runtime: failed to allocate WebAssembly goroutine stack") + } if !wasmSched.runq.Push(gp) { fatal("runtime: invalid run queue insertion") } } -func initWasmFiber(gp *g, stackSize uintptr) { +func initWasmFiber(gp *g, stackSize uintptr) bool { if stackSize == 0 { stackSize = defaultWasmGStackSize } @@ -106,8 +111,16 @@ func initWasmFiber(gp *g, stackSize uintptr) { } platform := &gp.context.platform - platform.stack = allocWasmStack(stackSize) - platform.asyncifyStack = allocWasmStack(asyncifySize) + platform.stack = AllocRoot(stackSize) + if platform.stack == nil { + return false + } + platform.asyncifyStack = AllocRoot(asyncifySize) + if platform.asyncifyStack == nil { + FreeRoot(platform.stack) + platform.stack = nil + return false + } platform.fiber.Init( emscripten.FiberEntry(wasmGStart), unsafe.Pointer(gp), @@ -116,6 +129,7 @@ func initWasmFiber(gp *g, stackSize uintptr) { platform.asyncifyStack, asyncifySize, ) + return true } func alignWasmStackSize(size uintptr) uintptr { From 6e2f5757676756d80ce6561795d2138cb22435e1 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Sun, 9 Aug 2026 22:38:07 +0800 Subject: [PATCH 08/18] runtime: keep wasm locality owner across Goexit --- runtime/internal/runtime/os_pthread.go | 1 + runtime/internal/runtime/z_default.go | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/internal/runtime/os_pthread.go b/runtime/internal/runtime/os_pthread.go index 7d2585a1b3..28121f8d60 100644 --- a/runtime/internal/runtime/os_pthread.go +++ b/runtime/internal/runtime/os_pthread.go @@ -66,6 +66,7 @@ func initThreadAttr(attr *pthread.Attr, stackSize uintptr) c.Int { } func goexitBackend(gp *g) { + leaveCurrentLocalContext() mexit(gp.m) pthread.Exit(nil) } diff --git a/runtime/internal/runtime/z_default.go b/runtime/internal/runtime/z_default.go index b322ae4d0e..b76139b86a 100644 --- a/runtime/internal/runtime/z_default.go +++ b/runtime/internal/runtime/z_default.go @@ -36,7 +36,6 @@ func Rethrow(link *Defer) { if gp.isMain { markMainExited() } - leaveCurrentLocalContext() goexitBackend(gp) } } From 4f056fa1dcec55a981e70d630f8e466e66e68f85 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Tue, 28 Jul 2026 02:33:38 +0800 Subject: [PATCH 09/18] runtime/wasm: add WASI single-worker scheduler --- internal/build/build.go | 22 +- internal/build/main_module.go | 37 ++- internal/build/wasm_postlink.go | 90 ++++++ internal/crosscompile/crosscompile.go | 31 +- runtime/internal/runtime/g_tls.go | 2 +- runtime/internal/runtime/g_wasm.go | 2 +- runtime/internal/runtime/os_pthread.go | 2 +- runtime/internal/runtime/os_wasm.go | 2 +- runtime/internal/runtime/proc_pthread.go | 2 +- runtime/internal/runtime/proc_wasip1.go | 272 ++++++++++++++++++ runtime/internal/runtime/proc_wasm.go | 14 +- runtime/internal/wasmcontext/context_js.go | 51 ++++ .../internal/wasmcontext/context_wasip1.go | 72 +++++ runtime/internal/wasmcontext/context_wasm.S | 121 ++++++++ runtime/internal/wasmcontext/doc.go | 19 ++ 15 files changed, 711 insertions(+), 28 deletions(-) create mode 100644 internal/build/wasm_postlink.go create mode 100644 runtime/internal/runtime/proc_wasip1.go create mode 100644 runtime/internal/wasmcontext/context_js.go create mode 100644 runtime/internal/wasmcontext/context_wasip1.go create mode 100644 runtime/internal/wasmcontext/context_wasm.S create mode 100644 runtime/internal/wasmcontext/doc.go diff --git a/internal/build/build.go b/internal/build/build.go index 65f742d52c..ea065818ed 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -1500,11 +1500,25 @@ func linkMainPkg(ctx *context, pkg *packages.Package, pkgs []*aPackage, outputPa } linkArgs = append(linkArgs, cSharedExportArgs(ctx, linkedOrder)...) - err = linkObjFiles(ctx, outputPath, linkInputs, linkArgs, verbose) - if err != nil { + linkOutput := outputPath + if needsWasmPostLink(ctx.buildConf, &ctx.crossCompile) { + tmp, err := os.CreateTemp(filepath.Dir(outputPath), "."+filepath.Base(outputPath)+".linked-*") + if err != nil { + return err + } + linkOutput = tmp.Name() + if err := tmp.Close(); err != nil { + os.Remove(linkOutput) + return err + } + defer os.Remove(linkOutput) + } + if err := linkObjFiles(ctx, linkOutput, linkInputs, linkArgs, verbose); err != nil { return err } - + if linkOutput != outputPath { + return postLinkWasm(ctx, linkOutput, outputPath, verbose) + } return nil } @@ -2684,7 +2698,7 @@ func shouldRunLLVMPasses(mode Mode) bool { } func IsWasiThreadsEnabled() bool { - return isEnvOn(llgoWasiThreads, true) + return isEnvOn(llgoWasiThreads, false) } func IsFullRpathEnabled() bool { diff --git a/internal/build/main_module.go b/internal/build/main_module.go index 2693cb8ed4..52b0f6efe1 100644 --- a/internal/build/main_module.go +++ b/internal/build/main_module.go @@ -133,10 +133,16 @@ func genMainModule(ctx *context, rtPkgPath string, pkg *packages.Package, cfg *g return mainAPkg } + var wasmRunMain llssa.Function + if ctx.crossCompile.WasmPostLink.Asyncify { + defineWasmMainTask(mainPkg, packageInits, mainInit, mainMain) + wasmRunMain = declareNoArgFunc(mainPkg, rtPkgPath+".RunWasmMain") + } entryFn := defineEntryFunction(ctx, mainPkg, argcVar, argvVar, argvValueType, entryFunctions{ runtimeStub: runtimeStub, mainInit: mainInit, mainMain: mainMain, + wasmRunMain: wasmRunMain, pyInit: pyInit, pyFinalize: pyFinalize, rtInit: rtInit, @@ -232,6 +238,7 @@ type entryFunctions struct { runtimeStub llssa.Function mainInit llssa.Function mainMain llssa.Function + wasmRunMain llssa.Function pyInit llssa.Function pyFinalize llssa.Function rtInit llssa.Function @@ -280,11 +287,15 @@ func defineEntryFunction(ctx *context, pkg llssa.Package, argcVar, argvVar llssa b.Call(fns.abiInit.Expr) } b.Call(fns.runtimeStub.Expr) - for _, init := range fns.packageInits { - b.Call(init.Expr) + if fns.wasmRunMain != nil { + b.Call(fns.wasmRunMain.Expr) + } else { + for _, init := range fns.packageInits { + b.Call(init.Expr) + } + b.Call(fns.mainInit.Expr) + b.Call(fns.mainMain.Expr) } - b.Call(fns.mainInit.Expr) - b.Call(fns.mainMain.Expr) if fns.pyFinalize != nil { b.Call(fns.pyFinalize.Expr) } @@ -295,6 +306,24 @@ func defineEntryFunction(ctx *context, pkg llssa.Package, argcVar, argvVar llssa return fn } +func defineWasmMainTask(pkg llssa.Package, packageInits []llssa.Function, mainInit, mainMain llssa.Function) { + prog := pkg.Prog + sig := newSignature( + []types.Type{types.Typ[types.UnsafePointer]}, + []types.Type{types.Typ[types.UnsafePointer]}, + ) + fn := pkg.NewFunc("__llgo_wasm_main", sig, llssa.InC) + fnVal := pkg.Module().NamedFunction("__llgo_wasm_main") + fnVal.SetVisibility(llvm.HiddenVisibility) + b := fn.MakeBody(1) + for _, init := range packageInits { + b.Call(init.Expr) + } + b.Call(mainInit.Expr) + b.Call(mainMain.Expr) + b.Return(prog.Nil(prog.VoidPtr())) +} + func defineStart(pkg llssa.Package, entry llssa.Function, argvType llssa.Type) { fn := pkg.NewFunc("_start", llssa.NoArgsNoRet, llssa.InC) pkg.Module().NamedFunction("_start").SetLinkage(llvm.WeakAnyLinkage) diff --git a/internal/build/wasm_postlink.go b/internal/build/wasm_postlink.go new file mode 100644 index 0000000000..99c9208c6f --- /dev/null +++ b/internal/build/wasm_postlink.go @@ -0,0 +1,90 @@ +//go:build !llgo + +/* + * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package build + +import ( + "fmt" + "os" + "os/exec" + "path/filepath" + + "github.com/goplus/llgo/internal/crosscompile" +) + +func needsWasmPostLink(conf *Config, target *crosscompile.Export) bool { + return conf != nil && conf.BuildMode == BuildModeExe && + target != nil && target.WasmPostLink.Asyncify +} + +func wasmPostLinkArgs(target *crosscompile.Export, input, output string, debug bool) []string { + if target == nil || !target.WasmPostLink.Asyncify { + return nil + } + // LLVM 19 lowers Wasm SjLj through the legacy EH encoding. Asyncify + // understands that form; translate it only after instrumentation so the + // final module uses the standardized exnref-based EH instructions. + args := []string{"--asyncify", "--translate-to-exnref"} + if debug { + args = append(args, "-g") + } + return append(args, input, "-o", output) +} + +func postLinkWasm(ctx *context, input, output string, verbose bool) error { + wasmOpt := os.Getenv("WASMOPT") + if wasmOpt == "" { + wasmOpt = "wasm-opt" + } + resolved, err := exec.LookPath(wasmOpt) + if err != nil { + return fmt.Errorf("WebAssembly Asyncify requires wasm-opt; install Binaryen or set WASMOPT: %w", err) + } + + outDir := filepath.Dir(output) + tmp, err := os.CreateTemp(outDir, "."+filepath.Base(output)+".wasm-opt-*") + if err != nil { + return err + } + tmpName := tmp.Name() + if err := tmp.Close(); err != nil { + os.Remove(tmpName) + return err + } + defer os.Remove(tmpName) + + args := wasmPostLinkArgs( + &ctx.crossCompile, + input, + tmpName, + shouldEmitDebugInfo(ctx.buildConf, &ctx.crossCompile), + ) + if ctx.shouldPrintCommands(verbose) { + fmt.Fprintln(os.Stderr, resolved, args) + } + cmd := exec.Command(resolved, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + return fmt.Errorf("wasm-opt Asyncify failed: %w", err) + } + if err := os.Rename(tmpName, output); err != nil { + return err + } + return nil +} diff --git a/internal/crosscompile/crosscompile.go b/internal/crosscompile/crosscompile.go index 8e2c6943c0..69a42b93c6 100644 --- a/internal/crosscompile/crosscompile.go +++ b/internal/crosscompile/crosscompile.go @@ -42,11 +42,18 @@ type Export struct { FormatDetail string // For uf2, it's uf2FamilyID Emulator string // Emulator command template (e.g., "qemu-system-arm -M {} -kernel {}") DebugInfo DebugInfoPolicy + WasmPostLink WasmPostLink // Flashing/Debugging configuration Device flash.Device // Device configuration for flashing/debugging } +// WasmPostLink describes transformations required after the core module is +// linked. Build orchestration owns tool discovery and atomic output handling. +type WasmPostLink struct { + Asyncify bool +} + // DebugInfoPolicy describes how a selected linker handles debug information. // Build orchestration consumes this typed capability instead of inferring it // from a target name or linker executable. @@ -374,6 +381,9 @@ func useWithJSWasm32(goos, goarch string, wasiThreads, forceEspClang bool, level "-matomics", "-mbulk-memory", } + if wasiThreads { + export.CCFLAGS = append(export.CCFLAGS, "-pthread") + } export.CFLAGS = []string{ "-I" + includeDir, "-Qunused-arguments", @@ -381,12 +391,20 @@ func useWithJSWasm32(goos, goarch string, wasiThreads, forceEspClang bool, level } // Add WebAssembly linker flags export.LDFLAGS = append(export.LDFLAGS, export.CCFLAGS...) + export.LDFLAGS = append(export.LDFLAGS, "-fwasm-exceptions") + if ltoMode.Enabled() { + export.LDFLAGS = append(export.LDFLAGS, "-Wl,--mllvm=-wasm-enable-sjlj") + } + export.CCFLAGS = append( + export.CCFLAGS, + "-fwasm-exceptions", + "-mllvm", "-wasm-enable-sjlj", + ) export.LDFLAGS = append(export.LDFLAGS, []string{ "-Wno-override-module", "-Wl,--error-limit=0", "-L" + libDir, "-Wl,--allow-undefined", - "-Wl,--import-memory,", // unknown import: `env::memory` has not been defined "-Wl,--export-memory", "-Wl,--initial-memory=67108864", // 64MB "-mbulk-memory", @@ -403,22 +421,19 @@ func useWithJSWasm32(goos, goarch string, wasiThreads, forceEspClang bool, level "-lwasi-emulated-getpid", "-lwasi-emulated-process-clocks", "-lwasi-emulated-signal", - "-fwasm-exceptions", - "-mllvm", "-wasm-enable-sjlj", }...) export.LLVMTarget = "wasm32-unknown-wasip1" // Add thread support if enabled if wasiThreads { - export.CCFLAGS = append( - export.CCFLAGS, - "-pthread", - ) - export.LDFLAGS = append(export.LDFLAGS, export.CCFLAGS...) + export.BuildTags = append(export.BuildTags, "llgo.wasi_threads") export.LDFLAGS = append( export.LDFLAGS, + "-Wl,--import-memory", "-lwasi-emulated-pthread", "-lpthread", ) + } else { + export.WasmPostLink.Asyncify = true } case "js": diff --git a/runtime/internal/runtime/g_tls.go b/runtime/internal/runtime/g_tls.go index 7300e4be6e..53e440d6bb 100644 --- a/runtime/internal/runtime/g_tls.go +++ b/runtime/internal/runtime/g_tls.go @@ -1,4 +1,4 @@ -//go:build llgo && !baremetal && (!js || !wasm) +//go:build llgo && !baremetal && (!wasm || (wasip1 && llgo.wasi_threads)) /* * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. diff --git a/runtime/internal/runtime/g_wasm.go b/runtime/internal/runtime/g_wasm.go index 9ec5a4f221..78746da0e5 100644 --- a/runtime/internal/runtime/g_wasm.go +++ b/runtime/internal/runtime/g_wasm.go @@ -1,4 +1,4 @@ -//go:build llgo && js && wasm +//go:build llgo && wasm && !(wasip1 && llgo.wasi_threads) /* * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. diff --git a/runtime/internal/runtime/os_pthread.go b/runtime/internal/runtime/os_pthread.go index 28121f8d60..5f600a8600 100644 --- a/runtime/internal/runtime/os_pthread.go +++ b/runtime/internal/runtime/os_pthread.go @@ -1,4 +1,4 @@ -//go:build !llgo || !js || !wasm +//go:build !llgo || !wasm || (wasip1 && llgo.wasi_threads) /* * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. diff --git a/runtime/internal/runtime/os_wasm.go b/runtime/internal/runtime/os_wasm.go index 06cb527227..78cdf9b626 100644 --- a/runtime/internal/runtime/os_wasm.go +++ b/runtime/internal/runtime/os_wasm.go @@ -1,4 +1,4 @@ -//go:build llgo && js && wasm +//go:build llgo && wasm && !(wasip1 && llgo.wasi_threads) /* * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. diff --git a/runtime/internal/runtime/proc_pthread.go b/runtime/internal/runtime/proc_pthread.go index 5aba69c90b..6eb579e703 100644 --- a/runtime/internal/runtime/proc_pthread.go +++ b/runtime/internal/runtime/proc_pthread.go @@ -1,4 +1,4 @@ -//go:build !llgo || !js || !wasm +//go:build !llgo || !wasm || (wasip1 && llgo.wasi_threads) /* * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. diff --git a/runtime/internal/runtime/proc_wasip1.go b/runtime/internal/runtime/proc_wasip1.go new file mode 100644 index 0000000000..513caab15e --- /dev/null +++ b/runtime/internal/runtime/proc_wasip1.go @@ -0,0 +1,272 @@ +//go:build llgo && wasip1 && wasm && !llgo.wasi_threads + +/* + * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package runtime + +import ( + "unsafe" + + "github.com/goplus/llgo/runtime/internal/runqueue" + "github.com/goplus/llgo/runtime/internal/wasmcontext" +) + +const ( + defaultWasmGStackSize = 64 << 10 + defaultWasmAsyncifyStackSize = 64 << 10 +) + +type runtimeContextPlatform struct { + context wasmcontext.Context + stack unsafe.Pointer + asyncifyStack unsafe.Pointer +} + +var wasmSched struct { + m m + p p + runq runqueue.Queue[*g] + started bool + mainExited bool +} + +func initRuntimeContext(ctx *runtimeContext, callergp *g, status uint32) *g { + gp := initG(ctx, callergp, status) + if status == _Grunning { + initWasmScheduler(gp) + } + return gp +} + +func initWasmScheduler(gp *g) { + if wasmSched.started { + fatal("runtime: WebAssembly scheduler initialized twice") + return + } + wasmSched.started = true + mp := &wasmSched.m + pp := &wasmSched.p + mp.curg = gp + mp.p = pp + mp.id = nextMid(mp) + pp.id = nextPid(pp) + setpstatus(pp, _Prunning) + pp.m = mp + gp.m = mp +} + +//go:linkname wasmMainTask __llgo_wasm_main +func wasmMainTask(unsafe.Pointer) unsafe.Pointer + +// RunWasmMain runs package initialization and main.main as the first +// Asyncify task. It remains on the system stack and dispatches one G at a time. +func RunWasmMain() { + gp := getg() + if gp == nil || !gp.isMain { + fatal("runtime: invalid WebAssembly main goroutine") + return + } + initWasmContext(gp, wasmcontext.Entry(wasmMainTask), nil, 0) + + for { + runWasmContext(gp) + status := readgstatus(gp) + if gp.isMain && status == _Grunning { + casgstatus(gp, _Grunning, _Gdead) + releaseWasmContext(gp) + return + } + releaseWasmOwnership(gp) + if status == _Gdead { + releaseWasmContext(gp) + } + + gp = wasmSched.runq.Pop() + if gp == nil { + if wasmSched.mainExited { + fatal("no goroutines (main called runtime.Goexit) - deadlock!") + } else { + fatal("all goroutines are asleep - deadlock!") + } + return + } + } +} + +func runWasmContext(gp *g) { + if readgstatus(gp) == _Grunnable { + casgstatus(gp, _Grunnable, _Grunning) + } + mp := &wasmSched.m + pp := &wasmSched.p + mp.curg = gp + pp.m = mp + gp.m = mp + setg(gp) + gp.context.platform.context.Resume() +} + +func releaseWasmOwnership(gp *g) { + if gp != nil { + gp.m = nil + } + wasmSched.m.curg = nil +} + +func newprocBackend(fn goroutineFunc, arg unsafe.Pointer, stackSize uintptr, callergp *g) { + gp := newproc1(fn, arg, callergp) + initWasmContext(gp, wasmcontext.Entry(wasmGStart), unsafe.Pointer(gp), stackSize) + if !wasmSched.runq.Push(gp) { + fatal("runtime: invalid run queue insertion") + } +} + +func initWasmContext(gp *g, entry wasmcontext.Entry, arg unsafe.Pointer, stackSize uintptr) { + if stackSize == 0 { + stackSize = defaultWasmGStackSize + } + stackSize = alignWasmStackSize(stackSize) + asyncifySize := uintptr(defaultWasmAsyncifyStackSize) + if stackSize > asyncifySize { + asyncifySize = stackSize + } + + platform := &gp.context.platform + platform.stack = allocWasmStack(stackSize) + platform.asyncifyStack = allocWasmStack(asyncifySize) + platform.context.Init( + entry, + arg, + platform.stack, + stackSize, + platform.asyncifyStack, + asyncifySize, + ) +} + +func alignWasmStackSize(size uintptr) uintptr { + const alignment = uintptr(16) + return (size + alignment - 1) &^ (alignment - 1) +} + +func allocWasmStack(size uintptr) unsafe.Pointer { + stack := AllocRoot(size) + if stack == nil { + panic("runtime: failed to allocate WebAssembly goroutine stack") + } + return stack +} + +func releaseWasmContext(gp *g) { + if gp == nil || gp.context == nil { + return + } + ctx := gp.context + platform := &ctx.platform + if platform.stack != nil { + FreeRoot(platform.stack) + platform.stack = nil + } + if platform.asyncifyStack != nil { + FreeRoot(platform.asyncifyStack) + platform.asyncifyStack = nil + } + freeRuntimeContext(ctx) +} + +func wasmGStart(arg unsafe.Pointer) unsafe.Pointer { + gp := (*g)(arg) + if gp == nil || getg() != gp { + fatal("runtime: invalid WebAssembly goroutine entry") + return nil + } + fn, fnarg := gp.startfn, gp.startarg + gp.startfn = nil + gp.startarg = nil + ret := fn(fnarg) + goexitBackend(gp) + return ret +} + +func goschedBackend() { + gp := getg() + casgstatus(gp, _Grunning, _Grunnable) + if !wasmSched.runq.Push(gp) { + fatal("runtime: invalid run queue insertion") + return + } + gp.context.platform.context.Suspend() +} + +func gopark() { + gp := getg() + casgstatus(gp, _Grunning, _Gwaiting) + gp.context.platform.context.Suspend() +} + +func goready(gp *g) { + if gp == nil { + fatal("runtime: ready of nil goroutine") + return + } + casgstatus(gp, _Gwaiting, _Grunnable) + if !wasmSched.runq.Push(gp) { + fatal("runtime: invalid run queue insertion") + } +} + +func goexitBackend(gp *g) { + casgstatus(gp, _Grunning, _Gdead) + if gp.isMain { + wasmSched.mainExited = true + } + gp.context.platform.context.Suspend() + fatal("runtime: resumed dead WebAssembly goroutine") +} + +// CurrentGForTesting returns an opaque handle suitable for ReadyForTesting. +func CurrentGForTesting() unsafe.Pointer { + return unsafe.Pointer(getg()) +} + +// ParkForTesting parks the current G until another G marks it runnable. +func ParkForTesting() { + gopark() +} + +// ReadyForTesting makes a G previously parked by ParkForTesting runnable. +func ReadyForTesting(handle unsafe.Pointer) { + goready((*g)(handle)) +} + +// SchedulerStateForTesting reports single-worker queue and ownership state. +func SchedulerStateForTesting() (runq uintptr, mid int64, pid int32) { + return wasmSched.runq.Len(), wasmSched.m.id, wasmSched.p.id +} + +func GMPForTesting() (goid, parentGoid uint64, mid int64, pid int32, gstatus, pstatus uint32, linked bool) { + gp := getg() + if gp == nil || gp.m == nil || gp.m.p == nil { + return + } + mp := gp.m + pp := mp.p + ctx := gp.context + return gp.goid, gp.parentGoid, mp.id, pp.id, readgstatus(gp), readpstatus(pp), + mp == &wasmSched.m && pp == &wasmSched.p && + mp.curg == gp && pp.m == mp && ctx != nil && &ctx.g == gp +} diff --git a/runtime/internal/runtime/proc_wasm.go b/runtime/internal/runtime/proc_wasm.go index f5ae5d71b5..383f47a149 100644 --- a/runtime/internal/runtime/proc_wasm.go +++ b/runtime/internal/runtime/proc_wasm.go @@ -21,8 +21,8 @@ package runtime import ( "unsafe" - "github.com/goplus/llgo/runtime/internal/clite/emscripten" "github.com/goplus/llgo/runtime/internal/runqueue" + "github.com/goplus/llgo/runtime/internal/wasmcontext" ) const ( @@ -31,7 +31,7 @@ const ( ) type runtimeContextPlatform struct { - fiber emscripten.Fiber + context wasmcontext.Context stack unsafe.Pointer asyncifyStack unsafe.Pointer runqNext *g @@ -121,8 +121,8 @@ func initWasmFiber(gp *g, stackSize uintptr) bool { platform.stack = nil return false } - platform.fiber.Init( - emscripten.FiberEntry(wasmGStart), + platform.context.Init( + wasmcontext.Entry(wasmGStart), unsafe.Pointer(gp), platform.stack, stackSize, @@ -151,7 +151,7 @@ func ensureCurrentWasmFiber(gp *g) { return } platform.asyncifyStack = allocWasmStack(defaultWasmAsyncifyStackSize) - platform.fiber.InitCurrent(platform.asyncifyStack, defaultWasmAsyncifyStackSize) + platform.context.InitCurrent(platform.asyncifyStack, defaultWasmAsyncifyStackSize) } func wasmGStart(arg unsafe.Pointer) { @@ -222,7 +222,7 @@ func resumeWasmG(old, next *g) { next.m = mp mp.curg = next setg(next) - old.context.platform.fiber.Swap(&next.context.platform.fiber) + old.context.platform.context.Swap(&next.context.platform.context) reapRetiredWasmG() } @@ -256,7 +256,7 @@ func resumeDeadWasmG(old, next *g) { next.m = mp mp.curg = next setg(next) - old.context.platform.fiber.Swap(&next.context.platform.fiber) + old.context.platform.context.Swap(&next.context.platform.context) fatal("runtime: resumed dead WebAssembly goroutine") } diff --git a/runtime/internal/wasmcontext/context_js.go b/runtime/internal/wasmcontext/context_js.go new file mode 100644 index 0000000000..8def06f26b --- /dev/null +++ b/runtime/internal/wasmcontext/context_js.go @@ -0,0 +1,51 @@ +//go:build llgo && js && wasm + +/* + * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package wasmcontext + +import ( + "unsafe" + + "github.com/goplus/llgo/runtime/internal/clite/emscripten" +) + +type Entry = emscripten.FiberEntry + +// Context wraps the Emscripten Fiber ABI used by JavaScript hosts. +type Context struct { + fiber emscripten.Fiber +} + +func (ctx *Context) Init(entry Entry, arg, stack unsafe.Pointer, stackSize uintptr, asyncifyStack unsafe.Pointer, asyncifyStackSize uintptr) { + ctx.fiber.Init( + entry, + arg, + stack, + stackSize, + asyncifyStack, + asyncifyStackSize, + ) +} + +func (ctx *Context) InitCurrent(asyncifyStack unsafe.Pointer, asyncifyStackSize uintptr) { + ctx.fiber.InitCurrent(asyncifyStack, asyncifyStackSize) +} + +func (ctx *Context) Swap(next *Context) { + ctx.fiber.Swap(&next.fiber) +} diff --git a/runtime/internal/wasmcontext/context_wasip1.go b/runtime/internal/wasmcontext/context_wasip1.go new file mode 100644 index 0000000000..4226e510bf --- /dev/null +++ b/runtime/internal/wasmcontext/context_wasip1.go @@ -0,0 +1,72 @@ +//go:build llgo && wasip1 && wasm && !llgo.wasi_threads + +/* + * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package wasmcontext + +import ( + "unsafe" + + c "github.com/goplus/llgo/runtime/internal/clite" +) + +//llgo:type C +type Entry func(unsafe.Pointer) unsafe.Pointer + +// Context is the state consumed by Binaryen Asyncify. The first five fields +// have fixed wasm32 offsets shared with context_wasm.S. +type Context struct { + entry unsafe.Pointer + arg unsafe.Pointer + asyncifyStack unsafe.Pointer + asyncifyEnd unsafe.Pointer + stackPointer unsafe.Pointer + launched bool +} + +func (ctx *Context) Init(entry Entry, arg, stack unsafe.Pointer, stackSize uintptr, asyncifyStack unsafe.Pointer, asyncifyStackSize uintptr) { + ctx.entry = c.Func(entry) + ctx.arg = arg + ctx.asyncifyStack = asyncifyStack + ctx.asyncifyEnd = unsafe.Add(asyncifyStack, asyncifyStackSize) + ctx.stackPointer = unsafe.Add(stack, stackSize) + ctx.launched = false +} + +func (ctx *Context) Resume() { + if !ctx.launched { + contextLaunch(ctx) + ctx.launched = true + return + } + contextRewind(ctx) +} + +func (ctx *Context) Suspend() { + contextUnwind(ctx) +} + +//go:linkname contextLaunch C.__llgo_wasm_context_launch +func contextLaunch(*Context) + +//go:linkname contextRewind C.__llgo_wasm_context_rewind +func contextRewind(*Context) + +//go:linkname contextUnwind C.__llgo_wasm_context_unwind +func contextUnwind(*Context) + +const LLGoFiles = "context_wasm.S" diff --git a/runtime/internal/wasmcontext/context_wasm.S b/runtime/internal/wasmcontext/context_wasm.S new file mode 100644 index 0000000000..bc3b33034c --- /dev/null +++ b/runtime/internal/wasmcontext/context_wasm.S @@ -0,0 +1,121 @@ +// Copyright (c) 2018-2026 The TinyGo Authors. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of the copyright holder nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// This file was adapted for LLGo's wasmcontext ABI and wasm32 WASI scheduler. + +.globaltype __stack_pointer, i32 + +.functype start_unwind (i32) -> () +.import_module start_unwind, asyncify +.import_name start_unwind, start_unwind +.functype stop_unwind () -> () +.import_module stop_unwind, asyncify +.import_name stop_unwind, stop_unwind +.functype start_rewind (i32) -> () +.import_module start_rewind, asyncify +.import_name start_rewind, start_rewind +.functype stop_rewind () -> () +.import_module stop_rewind, asyncify +.import_name stop_rewind, stop_rewind + +.global __llgo_wasm_context_unwind +.hidden __llgo_wasm_context_unwind +.type __llgo_wasm_context_unwind,@function +__llgo_wasm_context_unwind: + .functype __llgo_wasm_context_unwind (i32) -> () + i32.const 0 + i32.load8_u __llgo_wasm_context_rewinding + if + call stop_rewind + i32.const 0 + i32.const 0 + i32.store8 __llgo_wasm_context_rewinding + else + local.get 0 + global.get __stack_pointer + i32.store 16 + local.get 0 + i32.const 8 + i32.add + call start_unwind + end_if + return + end_function + +.global __llgo_wasm_context_launch +.hidden __llgo_wasm_context_launch +.type __llgo_wasm_context_launch,@function +__llgo_wasm_context_launch: + .functype __llgo_wasm_context_launch (i32) -> () + global.get __stack_pointer + local.get 0 + i32.load 16 + global.set __stack_pointer + local.get 0 + i32.load 4 + local.get 0 + i32.load 0 + call_indirect (i32) -> (i32) + drop + call stop_unwind + global.set __stack_pointer + return + end_function + +.global __llgo_wasm_context_rewind +.hidden __llgo_wasm_context_rewind +.type __llgo_wasm_context_rewind,@function +__llgo_wasm_context_rewind: + .functype __llgo_wasm_context_rewind (i32) -> () + global.get __stack_pointer + local.get 0 + i32.load 16 + global.set __stack_pointer + local.get 0 + i32.load 4 + local.get 0 + i32.load 0 + i32.const 0 + i32.const 1 + i32.store8 __llgo_wasm_context_rewinding + local.get 0 + i32.const 8 + i32.add + call start_rewind + call_indirect (i32) -> (i32) + drop + call stop_unwind + global.set __stack_pointer + return + end_function + +.hidden __llgo_wasm_context_rewinding +.type __llgo_wasm_context_rewinding,@object +.section .bss.__llgo_wasm_context_rewinding,"",@ +.globl __llgo_wasm_context_rewinding +__llgo_wasm_context_rewinding: + .int8 0 + .size __llgo_wasm_context_rewinding, 1 diff --git a/runtime/internal/wasmcontext/doc.go b/runtime/internal/wasmcontext/doc.go new file mode 100644 index 0000000000..688f6da9b7 --- /dev/null +++ b/runtime/internal/wasmcontext/doc.go @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package wasmcontext provides suspended execution contexts for WebAssembly +// runtime schedulers. +package wasmcontext From e2099f07b6f6323d50ea9084fd0b6336f06c458f Mon Sep 17 00:00:00 2001 From: Li Jie Date: Tue, 28 Jul 2026 02:33:43 +0800 Subject: [PATCH 10/18] test(runtime): exercise WASI Asyncify scheduler --- .github/actions/setup-binaryen/action.yml | 25 +++ .github/workflows/llgo.yml | 39 ++++- internal/build/build_test.go | 11 ++ internal/build/main_module_test.go | 54 +++++++ .../build/testdata/wasm-scheduler/main.go | 15 ++ .../build/testdata/wasm-scheduler/model_go.go | 14 +- internal/build/wasm_postlink_test.go | 145 ++++++++++++++++++ internal/crosscompile/crosscompile_test.go | 32 ++++ 8 files changed, 333 insertions(+), 2 deletions(-) create mode 100644 .github/actions/setup-binaryen/action.yml create mode 100644 internal/build/wasm_postlink_test.go diff --git a/.github/actions/setup-binaryen/action.yml b/.github/actions/setup-binaryen/action.yml new file mode 100644 index 0000000000..c5c8c41163 --- /dev/null +++ b/.github/actions/setup-binaryen/action.yml @@ -0,0 +1,25 @@ +name: "Setup Binaryen" +description: "Install a pinned Binaryen release" +inputs: + version: + description: "Binaryen release version" + required: false + default: "131" + +runs: + using: "composite" + steps: + - name: Install Binaryen + shell: bash + run: | + set -euo pipefail + + version="${{ inputs.version }}" + archive="binaryen-version_${version}-x86_64-linux.tar.gz" + base_url="https://github.com/WebAssembly/binaryen/releases/download/version_${version}" + cd "$RUNNER_TEMP" + curl --retry 3 --retry-all-errors -fsSLO "${base_url}/${archive}" + curl --retry 3 --retry-all-errors -fsSLO "${base_url}/${archive}.sha256" + sha256sum --check "${archive}.sha256" + tar -xzf "$archive" -C "$RUNNER_TEMP" + echo "$RUNNER_TEMP/binaryen-version_${version}/bin" >> "$GITHUB_PATH" diff --git a/.github/workflows/llgo.yml b/.github/workflows/llgo.yml index af30af60ca..b9a8a500d2 100644 --- a/.github/workflows/llgo.yml +++ b/.github/workflows/llgo.yml @@ -326,6 +326,9 @@ jobs: - name: Set up Go for building llgo uses: ./.github/actions/setup-go + - name: Set up Binaryen + uses: ./.github/actions/setup-binaryen + - name: Install wamr run: | git clone --branch WAMR-2.4.4 --depth 1 https://github.com/bytecodealliance/wasm-micro-runtime.git @@ -379,6 +382,19 @@ jobs: with: node-version: "25" + - name: Set up Binaryen + uses: ./.github/actions/setup-binaryen + + - name: Set up Wasmtime + uses: bytecodealliance/actions/wasmtime/setup@v1 + with: + version: "39.0.1" + + - name: Set up wasm-tools + uses: bytecodealliance/actions/wasm-tools/setup@v1 + with: + version: "1.243.0" + - name: Set up Go for building llgo uses: ./.github/actions/setup-go @@ -406,10 +422,31 @@ jobs: grep -Fq "fatal error: all goroutines are asleep - deadlock!" <<<"$output" } + run_wasi_scheduler() { + local module="$1" + local output + wasm-tools validate --features all "$module" + output=$(wasmtime run -W exceptions=y "$module" 2>&1) + grep -Fq "wasm scheduler ok" <<<"$output" + if output=$(wasmtime run -W exceptions=y \ + --env LLGO_WASM_SCHEDULER_DEADLOCK=1 "$module" 2>&1); then + echo "deadlock scheduler fixture unexpectedly succeeded" + return 1 + fi + grep -Fq "fatal error: all goroutines are asleep - deadlock!" <<<"$output" + } + GOOS=js GOARCH=wasm llgo build -o "$RUNNER_TEMP/runtime-js.wasm" ./internal/build/testdata/wasm-runtime GOOS=wasip1 GOARCH=wasm llgo build -o "$RUNNER_TEMP/runtime-wasip1.wasm" ./internal/build/testdata/wasm-runtime + LLGO_WASI_THREADS=1 GOOS=wasip1 GOARCH=wasm llgo build \ + -o "$RUNNER_TEMP/runtime-wasip1-threads.wasm" ./internal/build/testdata/wasm-runtime + test "$(wasmtime run -W exceptions=y "$RUNNER_TEMP/runtime-wasip1.wasm" 2>&1)" = "wasip1" GOOS=js GOARCH=wasm llgo build -o "$RUNNER_TEMP/wasm-scheduler-go.mjs" ./internal/build/testdata/wasm-scheduler run_wasm_scheduler "$RUNNER_TEMP/wasm-scheduler-go.mjs" llgo build -target wasm -o "$RUNNER_TEMP/wasm-scheduler.mjs" ./internal/build/testdata/wasm-scheduler run_wasm_scheduler "$RUNNER_TEMP/wasm-scheduler.mjs" - file "$RUNNER_TEMP/runtime-js.wasm" "$RUNNER_TEMP/runtime-wasip1.wasm" + GOOS=wasip1 GOARCH=wasm llgo build -o "$RUNNER_TEMP/wasm-scheduler-wasip1.wasm" ./internal/build/testdata/wasm-scheduler + run_wasi_scheduler "$RUNNER_TEMP/wasm-scheduler-wasip1.wasm" + file "$RUNNER_TEMP/runtime-js.wasm" \ + "$RUNNER_TEMP/runtime-wasip1.wasm" \ + "$RUNNER_TEMP/runtime-wasip1-threads.wasm" diff --git a/internal/build/build_test.go b/internal/build/build_test.go index fcdbbabd2b..b4482f28d7 100644 --- a/internal/build/build_test.go +++ b/internal/build/build_test.go @@ -1206,6 +1206,17 @@ func TestApplyBuildModeCompileFlags(t *testing.T) { applyBuildModeCompileFlags(BuildModeCShared, nil) } +func TestWASIThreadsAreOptIn(t *testing.T) { + t.Setenv(llgoWasiThreads, "") + if IsWasiThreadsEnabled() { + t.Fatal("WASI threads are enabled by default") + } + t.Setenv(llgoWasiThreads, "1") + if !IsWasiThreadsEnabled() { + t.Fatal("WASI threads opt-in was ignored") + } +} + func TestCHeaderPackagesExcludesStandardRuntime(t *testing.T) { prog := llssa.NewProgram(nil) defer prog.Dispose() diff --git a/internal/build/main_module_test.go b/internal/build/main_module_test.go index a3e3bcf73d..71237e03cf 100644 --- a/internal/build/main_module_test.go +++ b/internal/build/main_module_test.go @@ -11,6 +11,7 @@ import ( "strings" "testing" + "github.com/goplus/llgo/internal/crosscompile" "github.com/xgo-dev/llvm" "github.com/goplus/llgo/internal/packages" @@ -195,6 +196,59 @@ func TestLinkMainPkgRejectsPackageInitCycle(t *testing.T) { } } +func TestGenMainModuleWASIAsyncifyEntry(t *testing.T) { + llvm.InitializeAllTargets() + t.Setenv(llgoStdioNobuf, "") + ctx := &context{ + prog: llssa.NewProgram(nil), + buildConf: &Config{ + BuildMode: BuildModeExe, + Goos: "wasip1", + Goarch: "wasm", + }, + crossCompile: crosscompile.Export{ + WasmPostLink: crosscompile.WasmPostLink{Asyncify: true}, + }, + } + pkg := &packages.Package{PkgPath: "example.com/foo", ExportFile: "foo.a"} + mod := genMainModule(ctx, llssa.PkgRuntime, pkg, &genConfig{ + rtInit: true, + packageInits: []string{"example.com/dependency.init"}, + }) + ir := mod.LPkg.String() + checks := []string{ + `define hidden ptr @__llgo_wasm_main(ptr %0)`, + `call void @"example.com/dependency.init"()`, + `call void @"example.com/foo.init"()`, + `call void @"example.com/foo.main"()`, + `call void @"github.com/goplus/llgo/runtime/internal/runtime.RunWasmMain"()`, + } + for _, want := range checks { + if !strings.Contains(ir, want) { + t.Fatalf("WASI main module IR missing %q:\n%s", want, ir) + } + } + taskStart := strings.Index(ir, "define hidden ptr @__llgo_wasm_main(") + task := ir[taskStart:] + task = task[:strings.Index(task, "}\n")+2] + assertInOrder(t, task, + `call void @"example.com/dependency.init"()`, + `call void @"example.com/foo.init"()`, + `call void @"example.com/foo.main"()`, + ) + entryStart := strings.Index(ir, "define hidden i32 @__main_argc_argv(") + if entryStart < 0 { + t.Fatalf("WASI main module missing host entry:\n%s", ir) + } + entry := ir[entryStart:] + entry = entry[:strings.Index(entry, "}\n")+2] + if strings.Contains(entry, `call void @"example.com/dependency.init"()`) || + strings.Contains(entry, `call void @"example.com/foo.init"()`) || + strings.Contains(entry, `call void @"example.com/foo.main"()`) { + t.Fatalf("WASI system-stack entry calls package main directly:\n%s", entry) + } +} + func TestGenMainModuleLibrary(t *testing.T) { llvm.InitializeAllTargets() t.Setenv(llgoStdioNobuf, "") diff --git a/internal/build/testdata/wasm-scheduler/main.go b/internal/build/testdata/wasm-scheduler/main.go index 12fd8c81ae..399a5b39c5 100644 --- a/internal/build/testdata/wasm-scheduler/main.go +++ b/internal/build/testdata/wasm-scheduler/main.go @@ -30,6 +30,7 @@ var ( eventLog [8]int eventCount int done int + lifecycle int ) func event(value int) { @@ -134,9 +135,23 @@ func main() { if seenGCount != len(seenG) { panic("not all goroutines ran") } + testGoroutineLifecycle() println("wasm scheduler ok") } +func testGoroutineLifecycle() { + const count = 5000 + for i := 1; i <= count; i++ { + want := i + go func() { + lifecycle = want + }() + for lifecycle != want { + runtime.Gosched() + } + } +} + func testParkedMainDeadlock() { go func() {}() parkForTesting() diff --git a/internal/build/testdata/wasm-scheduler/model_go.go b/internal/build/testdata/wasm-scheduler/model_go.go index 73781131c4..9cde6cc3f5 100644 --- a/internal/build/testdata/wasm-scheduler/model_go.go +++ b/internal/build/testdata/wasm-scheduler/model_go.go @@ -2,9 +2,21 @@ package main -import "unsafe" +import ( + "runtime" + "unsafe" +) func checkWasmModel() { + if runtime.GOOS == "wasip1" { + if unsafe.Sizeof(uintptr(0)) != 4 { + panic("GOOS=wasip1 GOARCH=wasm must use 32-bit words") + } + if cLongSize() != 4 { + panic("GOOS=wasip1 GOARCH=wasm must use the wasm32 C data model") + } + return + } if unsafe.Sizeof(uintptr(0)) != 8 { panic("GOOS/GOARCH wasm must use 64-bit words") } diff --git a/internal/build/wasm_postlink_test.go b/internal/build/wasm_postlink_test.go new file mode 100644 index 0000000000..67e037836d --- /dev/null +++ b/internal/build/wasm_postlink_test.go @@ -0,0 +1,145 @@ +//go:build !llgo + +/* + * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package build + +import ( + "os" + "path/filepath" + "reflect" + "runtime" + "strings" + "testing" + + "github.com/goplus/llgo/internal/crosscompile" +) + +func TestWasmPostLinkArgs(t *testing.T) { + target := &crosscompile.Export{WasmPostLink: crosscompile.WasmPostLink{Asyncify: true}} + if got, want := wasmPostLinkArgs(target, "in.wasm", "out.wasm", false), + []string{"--asyncify", "--translate-to-exnref", "in.wasm", "-o", "out.wasm"}; !reflect.DeepEqual(got, want) { + t.Fatalf("wasmPostLinkArgs() = %v, want %v", got, want) + } + if got, want := wasmPostLinkArgs(target, "in.wasm", "out.wasm", true), + []string{"--asyncify", "--translate-to-exnref", "-g", "in.wasm", "-o", "out.wasm"}; !reflect.DeepEqual(got, want) { + t.Fatalf("wasmPostLinkArgs(debug) = %v, want %v", got, want) + } + if got := wasmPostLinkArgs(&crosscompile.Export{}, "in", "out", false); got != nil { + t.Fatalf("wasmPostLinkArgs(disabled) = %v, want nil", got) + } +} + +func TestNeedsWasmPostLink(t *testing.T) { + target := &crosscompile.Export{WasmPostLink: crosscompile.WasmPostLink{Asyncify: true}} + tests := []struct { + name string + conf *Config + want bool + }{ + {name: "executable", conf: &Config{BuildMode: BuildModeExe}, want: true}, + {name: "archive", conf: &Config{BuildMode: BuildModeCArchive}}, + {name: "shared", conf: &Config{BuildMode: BuildModeCShared}}, + {name: "nil config"}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + if got := needsWasmPostLink(test.conf, target); got != test.want { + t.Fatalf("needsWasmPostLink() = %v, want %v", got, test.want) + } + }) + } + if needsWasmPostLink(&Config{BuildMode: BuildModeExe}, nil) { + t.Fatal("needsWasmPostLink() enabled for a nil target") + } +} + +func TestPostLinkWasmPublishesOutput(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("test helper uses a POSIX shell") + } + dir := t.TempDir() + input := filepath.Join(dir, "linked.wasm") + output := filepath.Join(dir, "app.wasm") + argsFile := filepath.Join(dir, "args") + if err := os.WriteFile(input, []byte("core module"), 0o644); err != nil { + t.Fatal(err) + } + + tool := filepath.Join(dir, "wasm-opt") + script := `#!/bin/sh +printf '%s\n' "$@" > "$ARGS_FILE" +input= +output= +while [ "$#" -gt 0 ]; do + case "$1" in + -o) + output="$2" + shift 2 + ;; + -*) + shift + ;; + *) + input="$1" + shift + ;; + esac +done +cp "$input" "$output" +` + if err := os.WriteFile(tool, []byte(script), 0o755); err != nil { + t.Fatal(err) + } + t.Setenv("WASMOPT", tool) + t.Setenv("ARGS_FILE", argsFile) + + ctx := &context{ + buildConf: &Config{Mode: ModeBuild, LinkOptions: LinkOptions{DWARF: DWARFOmit}}, + crossCompile: crosscompile.Export{ + WasmPostLink: crosscompile.WasmPostLink{Asyncify: true}, + }, + } + if err := postLinkWasm(ctx, input, output, false); err != nil { + t.Fatal(err) + } + if data, err := os.ReadFile(output); err != nil || string(data) != "core module" { + t.Fatalf("published output = %q, %v", data, err) + } + args, err := os.ReadFile(argsFile) + if err != nil { + t.Fatal(err) + } + if got := string(args); !strings.Contains(got, "--asyncify\n--translate-to-exnref\n") || + !strings.Contains(got, input+"\n-o\n") { + t.Fatalf("wasm-opt args = %q", got) + } +} + +func TestPostLinkWasmReportsMissingTool(t *testing.T) { + t.Setenv("WASMOPT", filepath.Join(t.TempDir(), "missing-wasm-opt")) + ctx := &context{ + buildConf: &Config{}, + crossCompile: crosscompile.Export{ + WasmPostLink: crosscompile.WasmPostLink{Asyncify: true}, + }, + } + err := postLinkWasm(ctx, "input", filepath.Join(t.TempDir(), "output"), false) + if err == nil || !strings.Contains(err.Error(), "install Binaryen or set WASMOPT") { + t.Fatalf("postLinkWasm() error = %v", err) + } +} diff --git a/internal/crosscompile/crosscompile_test.go b/internal/crosscompile/crosscompile_test.go index 66be579201..0b3d7385f7 100644 --- a/internal/crosscompile/crosscompile_test.go +++ b/internal/crosscompile/crosscompile_test.go @@ -125,6 +125,16 @@ func TestUseCrossCompileSDK(t *testing.T) { if !hasResourceDir { t.Error("Missing -resource-dir flag in CCFLAGS") } + if !slices.Contains(export.CCFLAGS, "-fwasm-exceptions") || + !hasMllvmOption(export.CCFLAGS, "-wasm-enable-sjlj") { + t.Errorf("CCFLAGS do not enable WebAssembly SjLj lowering: %v", export.CCFLAGS) + } + if !export.WasmPostLink.Asyncify { + t.Error("WASI target does not request Asyncify post-link processing") + } + if slices.Contains(export.LDFLAGS, "-Wl,--import-memory") { + t.Errorf("single-worker WASI imports host memory: %v", export.LDFLAGS) + } } else if tc.name == "Same Platform" { // For same platform, we expect sysroot only on macOS if runtime.GOOS == "darwin" && !hasSysroot { @@ -173,6 +183,28 @@ func TestUseCrossCompileSDK(t *testing.T) { } } +func TestUseWASIThreadsImportsMemory(t *testing.T) { + if testing.Short() { + t.Skip("requires WASI SDK") + } + export, err := use("wasip1", "wasm", true, false, optlevel.O2, lto.Off, false) + if err != nil { + t.Fatal(err) + } + if !slices.Contains(export.CCFLAGS, "-pthread") { + t.Fatalf("CCFLAGS do not enable WASI threads: %v", export.CCFLAGS) + } + if !slices.Contains(export.BuildTags, "llgo.wasi_threads") { + t.Fatalf("BuildTags do not select the WASI pthread backend: %v", export.BuildTags) + } + if !slices.Contains(export.LDFLAGS, "-Wl,--import-memory") { + t.Fatalf("LDFLAGS do not import shared host memory: %v", export.LDFLAGS) + } + if export.WasmPostLink.Asyncify { + t.Fatal("WASI pthread mode requests single-worker Asyncify processing") + } +} + func TestUseJSSupportsNode(t *testing.T) { export, err := use("js", "wasm", false, false, optlevel.Oz, lto.Off, false) if err != nil { From 6ccbe7c043170a3b3e75539162ade1ae707d7e2a Mon Sep 17 00:00:00 2001 From: Li Jie Date: Tue, 28 Jul 2026 02:40:38 +0800 Subject: [PATCH 11/18] fix(runtime/wasm): initialize scheduler for minimal P1 mains --- internal/build/main_module.go | 2 +- internal/build/main_module_test.go | 2 +- runtime/internal/wasmcontext/{ => _asm}/context_wasm.S | 0 runtime/internal/wasmcontext/context_wasip1.go | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename runtime/internal/wasmcontext/{ => _asm}/context_wasm.S (100%) diff --git a/internal/build/main_module.go b/internal/build/main_module.go index 52b0f6efe1..2c011aee10 100644 --- a/internal/build/main_module.go +++ b/internal/build/main_module.go @@ -88,7 +88,7 @@ func genMainModule(ctx *context, rtPkgPath string, pkg *packages.Package, cfg *g } var rtInit llssa.Function - if cfg.rtInit { + if cfg.rtInit || ctx.crossCompile.WasmPostLink.Asyncify { rtInit = declareNoArgFunc(mainPkg, rtPkgPath+".init") } diff --git a/internal/build/main_module_test.go b/internal/build/main_module_test.go index 71237e03cf..8c86146809 100644 --- a/internal/build/main_module_test.go +++ b/internal/build/main_module_test.go @@ -212,12 +212,12 @@ func TestGenMainModuleWASIAsyncifyEntry(t *testing.T) { } pkg := &packages.Package{PkgPath: "example.com/foo", ExportFile: "foo.a"} mod := genMainModule(ctx, llssa.PkgRuntime, pkg, &genConfig{ - rtInit: true, packageInits: []string{"example.com/dependency.init"}, }) ir := mod.LPkg.String() checks := []string{ `define hidden ptr @__llgo_wasm_main(ptr %0)`, + `call void @"github.com/goplus/llgo/runtime/internal/runtime.init"()`, `call void @"example.com/dependency.init"()`, `call void @"example.com/foo.init"()`, `call void @"example.com/foo.main"()`, diff --git a/runtime/internal/wasmcontext/context_wasm.S b/runtime/internal/wasmcontext/_asm/context_wasm.S similarity index 100% rename from runtime/internal/wasmcontext/context_wasm.S rename to runtime/internal/wasmcontext/_asm/context_wasm.S diff --git a/runtime/internal/wasmcontext/context_wasip1.go b/runtime/internal/wasmcontext/context_wasip1.go index 4226e510bf..63177044cc 100644 --- a/runtime/internal/wasmcontext/context_wasip1.go +++ b/runtime/internal/wasmcontext/context_wasip1.go @@ -69,4 +69,4 @@ func contextRewind(*Context) //go:linkname contextUnwind C.__llgo_wasm_context_unwind func contextUnwind(*Context) -const LLGoFiles = "context_wasm.S" +const LLGoFiles = "_asm/context_wasm.S" From 30b3bb09c6ddee410cc200aa1e03e727d9b115f1 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Tue, 28 Jul 2026 02:46:06 +0800 Subject: [PATCH 12/18] ci: install Binaryen for wasm cache tests --- .github/actions/setup-binaryen/action.yml | 19 +++++++++++++++++-- .github/workflows/build-cache.yml | 3 +++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/.github/actions/setup-binaryen/action.yml b/.github/actions/setup-binaryen/action.yml index c5c8c41163..ed76713576 100644 --- a/.github/actions/setup-binaryen/action.yml +++ b/.github/actions/setup-binaryen/action.yml @@ -15,11 +15,26 @@ runs: set -euo pipefail version="${{ inputs.version }}" - archive="binaryen-version_${version}-x86_64-linux.tar.gz" + case "$(uname -s):$(uname -m)" in + Linux:x86_64) platform="x86_64-linux" ;; + Linux:aarch64|Linux:arm64) platform="aarch64-linux" ;; + Darwin:x86_64) platform="x86_64-macos" ;; + Darwin:arm64) platform="arm64-macos" ;; + *) + echo "Unsupported Binaryen host: $(uname -s) $(uname -m)" >&2 + exit 1 + ;; + esac + + archive="binaryen-version_${version}-${platform}.tar.gz" base_url="https://github.com/WebAssembly/binaryen/releases/download/version_${version}" cd "$RUNNER_TEMP" curl --retry 3 --retry-all-errors -fsSLO "${base_url}/${archive}" curl --retry 3 --retry-all-errors -fsSLO "${base_url}/${archive}.sha256" - sha256sum --check "${archive}.sha256" + if command -v sha256sum >/dev/null; then + sha256sum --check "${archive}.sha256" + else + shasum -a 256 --check "${archive}.sha256" + fi tar -xzf "$archive" -C "$RUNNER_TEMP" echo "$RUNNER_TEMP/binaryen-version_${version}/bin" >> "$GITHUB_PATH" diff --git a/.github/workflows/build-cache.yml b/.github/workflows/build-cache.yml index 62429be59d..570d1e0260 100644 --- a/.github/workflows/build-cache.yml +++ b/.github/workflows/build-cache.yml @@ -33,6 +33,9 @@ jobs: - name: Set up Go uses: ./.github/actions/setup-go + - name: Set up Binaryen + uses: ./.github/actions/setup-binaryen + - name: Install wamr (for wasm tests) if: startsWith(matrix.os, 'macos') run: | From 7e1a98da77a0511006ef927c3c86bd9b3bb9e53a Mon Sep 17 00:00:00 2001 From: Li Jie Date: Tue, 28 Jul 2026 04:00:08 +0800 Subject: [PATCH 13/18] runtime/wasm: keep fiber host calls out of method roots --- runtime/internal/clite/emscripten/fiber.go | 12 ++++++------ runtime/internal/clite/emscripten/fiber_test.go | 7 +++++++ runtime/internal/wasmcontext/context_js.go | 7 ++++--- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/runtime/internal/clite/emscripten/fiber.go b/runtime/internal/clite/emscripten/fiber.go index 00a4fe87f7..95e6ae2410 100644 --- a/runtime/internal/clite/emscripten/fiber.go +++ b/runtime/internal/clite/emscripten/fiber.go @@ -29,14 +29,14 @@ type Fiber struct { //llgo:type C type FiberEntry func(c.Pointer) -// llgo:link (*Fiber).Init C.emscripten_fiber_init -func (fiber *Fiber) Init(entry FiberEntry, arg, stack c.Pointer, stackSize uintptr, asyncifyStack c.Pointer, asyncifyStackSize uintptr) { +// llgo:link FiberInit C.emscripten_fiber_init +func FiberInit(fiber *Fiber, entry FiberEntry, arg, stack c.Pointer, stackSize uintptr, asyncifyStack c.Pointer, asyncifyStackSize uintptr) { } -// llgo:link (*Fiber).InitCurrent C.emscripten_fiber_init_from_current_context -func (fiber *Fiber) InitCurrent(asyncifyStack c.Pointer, asyncifyStackSize uintptr) { +// llgo:link FiberInitCurrent C.emscripten_fiber_init_from_current_context +func FiberInitCurrent(fiber *Fiber, asyncifyStack c.Pointer, asyncifyStackSize uintptr) { } -// llgo:link (*Fiber).Swap C.emscripten_fiber_swap -func (fiber *Fiber) Swap(next *Fiber) { +// llgo:link FiberSwap C.emscripten_fiber_swap +func FiberSwap(fiber, next *Fiber) { } diff --git a/runtime/internal/clite/emscripten/fiber_test.go b/runtime/internal/clite/emscripten/fiber_test.go index da2456dbc1..42c41eb5bc 100644 --- a/runtime/internal/clite/emscripten/fiber_test.go +++ b/runtime/internal/clite/emscripten/fiber_test.go @@ -1,6 +1,7 @@ package emscripten import ( + "reflect" "testing" "unsafe" ) @@ -10,3 +11,9 @@ func TestFiberStorageUsesEightWords(t *testing.T) { t.Fatalf("Fiber size = %d, want %d", got, want) } } + +func TestFiberHasNoReflectableHostMethods(t *testing.T) { + if got := reflect.TypeOf(Fiber{}).NumMethod(); got != 0 { + t.Fatalf("Fiber has %d reflectable methods, want 0", got) + } +} diff --git a/runtime/internal/wasmcontext/context_js.go b/runtime/internal/wasmcontext/context_js.go index 8def06f26b..4550c38320 100644 --- a/runtime/internal/wasmcontext/context_js.go +++ b/runtime/internal/wasmcontext/context_js.go @@ -32,7 +32,8 @@ type Context struct { } func (ctx *Context) Init(entry Entry, arg, stack unsafe.Pointer, stackSize uintptr, asyncifyStack unsafe.Pointer, asyncifyStackSize uintptr) { - ctx.fiber.Init( + emscripten.FiberInit( + &ctx.fiber, entry, arg, stack, @@ -43,9 +44,9 @@ func (ctx *Context) Init(entry Entry, arg, stack unsafe.Pointer, stackSize uintp } func (ctx *Context) InitCurrent(asyncifyStack unsafe.Pointer, asyncifyStackSize uintptr) { - ctx.fiber.InitCurrent(asyncifyStack, asyncifyStackSize) + emscripten.FiberInitCurrent(&ctx.fiber, asyncifyStack, asyncifyStackSize) } func (ctx *Context) Swap(next *Context) { - ctx.fiber.Swap(&next.fiber) + emscripten.FiberSwap(&ctx.fiber, &next.fiber) } From 37ffa4e16ba12cb15d5933c3c4789c39b44b02e1 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Wed, 29 Jul 2026 01:05:32 +0800 Subject: [PATCH 14/18] build/wasm: centralize post-link output lifecycle --- internal/build/build.go | 21 +-- internal/build/wasm_postlink.go | 47 +++++- internal/build/wasm_postlink_test.go | 178 ++++++++++++++++----- internal/crosscompile/crosscompile_test.go | 13 ++ 4 files changed, 199 insertions(+), 60 deletions(-) diff --git a/internal/build/build.go b/internal/build/build.go index ea065818ed..11bf84fd88 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -1500,26 +1500,15 @@ func linkMainPkg(ctx *context, pkg *packages.Package, pkgs []*aPackage, outputPa } linkArgs = append(linkArgs, cSharedExportArgs(ctx, linkedOrder)...) - linkOutput := outputPath - if needsWasmPostLink(ctx.buildConf, &ctx.crossCompile) { - tmp, err := os.CreateTemp(filepath.Dir(outputPath), "."+filepath.Base(outputPath)+".linked-*") - if err != nil { - return err - } - linkOutput = tmp.Name() - if err := tmp.Close(); err != nil { - os.Remove(linkOutput) - return err - } - defer os.Remove(linkOutput) + linkOutput, err := prepareWasmLinkOutput(ctx.buildConf, &ctx.crossCompile, outputPath) + if err != nil { + return err } + defer cleanupWasmLinkOutput(linkOutput, outputPath) if err := linkObjFiles(ctx, linkOutput, linkInputs, linkArgs, verbose); err != nil { return err } - if linkOutput != outputPath { - return postLinkWasm(ctx, linkOutput, outputPath, verbose) - } - return nil + return publishWasmLinkOutput(ctx, linkOutput, outputPath, verbose) } func linkedPackageMetas(pkgs []Package) []*meta.PackageMeta { diff --git a/internal/build/wasm_postlink.go b/internal/build/wasm_postlink.go index 99c9208c6f..b460f153b1 100644 --- a/internal/build/wasm_postlink.go +++ b/internal/build/wasm_postlink.go @@ -46,6 +46,42 @@ func wasmPostLinkArgs(target *crosscompile.Export, input, output string, debug b return append(args, input, "-o", output) } +func prepareWasmLinkOutput(conf *Config, target *crosscompile.Export, output string) (string, error) { + if !needsWasmPostLink(conf, target) { + return output, nil + } + return createClosedTemp( + filepath.Dir(output), + "."+filepath.Base(output)+".linked-*", + ) +} + +func cleanupWasmLinkOutput(input, output string) { + if input != output { + os.Remove(input) + } +} + +func publishWasmLinkOutput(ctx *context, input, output string, verbose bool) error { + if input == output { + return nil + } + return postLinkWasm(ctx, input, output, verbose) +} + +func createClosedTemp(dir, pattern string) (string, error) { + tmp, err := os.CreateTemp(dir, pattern) + if err != nil { + return "", err + } + name := tmp.Name() + if err := tmp.Close(); err != nil { + os.Remove(name) + return "", err + } + return name, nil +} + func postLinkWasm(ctx *context, input, output string, verbose bool) error { wasmOpt := os.Getenv("WASMOPT") if wasmOpt == "" { @@ -56,16 +92,13 @@ func postLinkWasm(ctx *context, input, output string, verbose bool) error { return fmt.Errorf("WebAssembly Asyncify requires wasm-opt; install Binaryen or set WASMOPT: %w", err) } - outDir := filepath.Dir(output) - tmp, err := os.CreateTemp(outDir, "."+filepath.Base(output)+".wasm-opt-*") + tmpName, err := createClosedTemp( + filepath.Dir(output), + "."+filepath.Base(output)+".wasm-opt-*", + ) if err != nil { return err } - tmpName := tmp.Name() - if err := tmp.Close(); err != nil { - os.Remove(tmpName) - return err - } defer os.Remove(tmpName) args := wasmPostLinkArgs( diff --git a/internal/build/wasm_postlink_test.go b/internal/build/wasm_postlink_test.go index 67e037836d..0a00327425 100644 --- a/internal/build/wasm_postlink_test.go +++ b/internal/build/wasm_postlink_test.go @@ -29,6 +29,27 @@ import ( "github.com/goplus/llgo/internal/crosscompile" ) +func wasmPostLinkTestContext() *context { + return &context{ + buildConf: &Config{LinkOptions: LinkOptions{DWARF: DWARFOmit}}, + crossCompile: crosscompile.Export{ + WasmPostLink: crosscompile.WasmPostLink{Asyncify: true}, + }, + } +} + +func writeWasmOptTestTool(t *testing.T, dir, script string) string { + t.Helper() + if runtime.GOOS == "windows" { + t.Skip("test helper uses a POSIX shell") + } + tool := filepath.Join(dir, "wasm-opt") + if err := os.WriteFile(tool, []byte(script), 0o755); err != nil { + t.Fatal(err) + } + return tool +} + func TestWasmPostLinkArgs(t *testing.T) { target := &crosscompile.Export{WasmPostLink: crosscompile.WasmPostLink{Asyncify: true}} if got, want := wasmPostLinkArgs(target, "in.wasm", "out.wasm", false), @@ -68,10 +89,48 @@ func TestNeedsWasmPostLink(t *testing.T) { } } -func TestPostLinkWasmPublishesOutput(t *testing.T) { - if runtime.GOOS == "windows" { - t.Skip("test helper uses a POSIX shell") +func TestPrepareWasmLinkOutput(t *testing.T) { + dir := t.TempDir() + output := filepath.Join(dir, "app.wasm") + target := &crosscompile.Export{WasmPostLink: crosscompile.WasmPostLink{Asyncify: true}} + + input, err := prepareWasmLinkOutput(&Config{BuildMode: BuildModeExe}, target, output) + if err != nil { + t.Fatal(err) + } + if input == output || filepath.Dir(input) != dir { + t.Fatalf("temporary link output = %q, want a distinct file in %q", input, dir) + } + if _, err := os.Stat(input); err != nil { + t.Fatalf("temporary link output was not created: %v", err) + } + cleanupWasmLinkOutput(input, output) + if _, err := os.Stat(input); !os.IsNotExist(err) { + t.Fatalf("temporary link output remains after cleanup: %v", err) + } + + if err := os.WriteFile(output, []byte("final"), 0o644); err != nil { + t.Fatal(err) + } + input, err = prepareWasmLinkOutput(&Config{BuildMode: BuildModeCArchive}, target, output) + if err != nil || input != output { + t.Fatalf("disabled post-link output = %q, %v; want %q, nil", input, err, output) + } + cleanupWasmLinkOutput(input, output) + if data, err := os.ReadFile(output); err != nil || string(data) != "final" { + t.Fatalf("cleanup removed final output: %q, %v", data, err) + } + if err := publishWasmLinkOutput(nil, output, output, false); err != nil { + t.Fatalf("disabled publish failed: %v", err) } + + missingOutput := filepath.Join(dir, "missing", "app.wasm") + if _, err := prepareWasmLinkOutput(&Config{BuildMode: BuildModeExe}, target, missingOutput); err == nil { + t.Fatal("prepareWasmLinkOutput succeeded with a missing output directory") + } +} + +func TestPostLinkWasmPublishesOutput(t *testing.T) { dir := t.TempDir() input := filepath.Join(dir, "linked.wasm") output := filepath.Join(dir, "app.wasm") @@ -80,43 +139,35 @@ func TestPostLinkWasmPublishesOutput(t *testing.T) { t.Fatal(err) } - tool := filepath.Join(dir, "wasm-opt") script := `#!/bin/sh printf '%s\n' "$@" > "$ARGS_FILE" -input= -output= -while [ "$#" -gt 0 ]; do - case "$1" in - -o) - output="$2" - shift 2 - ;; - -*) - shift - ;; - *) - input="$1" - shift - ;; - esac -done -cp "$input" "$output" +cp "$3" "$5" ` - if err := os.WriteFile(tool, []byte(script), 0o755); err != nil { + tool := writeWasmOptTestTool(t, dir, script) + t.Setenv("WASMOPT", "") + t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH")) + t.Setenv("ARGS_FILE", argsFile) + + ctx := wasmPostLinkTestContext() + stderr, err := os.CreateTemp(dir, "stderr") + if err != nil { t.Fatal(err) } - t.Setenv("WASMOPT", tool) - t.Setenv("ARGS_FILE", argsFile) + oldStderr := os.Stderr + os.Stderr = stderr + t.Cleanup(func() { os.Stderr = oldStderr }) - ctx := &context{ - buildConf: &Config{Mode: ModeBuild, LinkOptions: LinkOptions{DWARF: DWARFOmit}}, - crossCompile: crosscompile.Export{ - WasmPostLink: crosscompile.WasmPostLink{Asyncify: true}, - }, + if err := publishWasmLinkOutput(ctx, input, output, true); err != nil { + t.Fatal(err) } - if err := postLinkWasm(ctx, input, output, false); err != nil { + if err := stderr.Close(); err != nil { t.Fatal(err) } + if got, err := os.ReadFile(stderr.Name()); err != nil || + !strings.Contains(string(got), tool) || + !strings.Contains(string(got), "--asyncify") { + t.Fatalf("verbose command = %q, %v", got, err) + } if data, err := os.ReadFile(output); err != nil || string(data) != "core module" { t.Fatalf("published output = %q, %v", data, err) } @@ -130,16 +181,69 @@ cp "$input" "$output" } } +func TestPostLinkWasmReportsToolFailure(t *testing.T) { + dir := t.TempDir() + input := filepath.Join(dir, "linked.wasm") + output := filepath.Join(dir, "app.wasm") + if err := os.WriteFile(input, []byte("new"), 0o644); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(output, []byte("old"), 0o644); err != nil { + t.Fatal(err) + } + tool := writeWasmOptTestTool(t, dir, "#!/bin/sh\nexit 7\n") + t.Setenv("WASMOPT", tool) + + ctx := wasmPostLinkTestContext() + err := postLinkWasm(ctx, input, output, false) + if err == nil || !strings.Contains(err.Error(), "wasm-opt Asyncify failed") { + t.Fatalf("postLinkWasm() error = %v", err) + } + if data, err := os.ReadFile(output); err != nil || string(data) != "old" { + t.Fatalf("failed post-link changed final output: %q, %v", data, err) + } +} + +func TestPostLinkWasmReportsPublishFailure(t *testing.T) { + dir := t.TempDir() + input := filepath.Join(dir, "linked.wasm") + output := filepath.Join(dir, "existing-directory") + if err := os.WriteFile(input, []byte("core module"), 0o644); err != nil { + t.Fatal(err) + } + if err := os.Mkdir(output, 0o755); err != nil { + t.Fatal(err) + } + script := "#!/bin/sh\ncp \"$3\" \"$5\"\n" + tool := writeWasmOptTestTool(t, dir, script) + t.Setenv("WASMOPT", tool) + + ctx := wasmPostLinkTestContext() + err := postLinkWasm(ctx, input, output, false) + if err == nil { + t.Fatal("postLinkWasm succeeded when the final output was a directory") + } + if strings.Contains(err.Error(), "wasm-opt Asyncify failed") { + t.Fatalf("postLinkWasm failed before publishing output: %v", err) + } +} + func TestPostLinkWasmReportsMissingTool(t *testing.T) { t.Setenv("WASMOPT", filepath.Join(t.TempDir(), "missing-wasm-opt")) - ctx := &context{ - buildConf: &Config{}, - crossCompile: crosscompile.Export{ - WasmPostLink: crosscompile.WasmPostLink{Asyncify: true}, - }, - } + ctx := wasmPostLinkTestContext() err := postLinkWasm(ctx, "input", filepath.Join(t.TempDir(), "output"), false) if err == nil || !strings.Contains(err.Error(), "install Binaryen or set WASMOPT") { t.Fatalf("postLinkWasm() error = %v", err) } } + +func TestPostLinkWasmReportsInvalidOutputDirectory(t *testing.T) { + dir := t.TempDir() + tool := writeWasmOptTestTool(t, dir, "") + t.Setenv("WASMOPT", tool) + ctx := wasmPostLinkTestContext() + err := postLinkWasm(ctx, "input", filepath.Join(dir, "missing", "output"), false) + if err == nil { + t.Fatal("postLinkWasm succeeded with a missing output directory") + } +} diff --git a/internal/crosscompile/crosscompile_test.go b/internal/crosscompile/crosscompile_test.go index 0b3d7385f7..f811bf3e9b 100644 --- a/internal/crosscompile/crosscompile_test.go +++ b/internal/crosscompile/crosscompile_test.go @@ -205,6 +205,19 @@ func TestUseWASIThreadsImportsMemory(t *testing.T) { } } +func TestUseWASILTOEnablesSjLjAtLink(t *testing.T) { + if testing.Short() { + t.Skip("requires WASI SDK") + } + export, err := use("wasip1", "wasm", false, false, optlevel.O2, lto.Thin, false) + if err != nil { + t.Fatal(err) + } + if !slices.Contains(export.LDFLAGS, "-Wl,--mllvm=-wasm-enable-sjlj") { + t.Fatalf("LDFLAGS do not enable Wasm SjLj for LTO: %v", export.LDFLAGS) + } +} + func TestUseJSSupportsNode(t *testing.T) { export, err := use("js", "wasm", false, false, optlevel.Oz, lto.Off, false) if err != nil { From 7c914463400d8e8bf828540b4cafd6ce1857d54d Mon Sep 17 00:00:00 2001 From: Li Jie Date: Thu, 30 Jul 2026 07:39:55 +0800 Subject: [PATCH 15/18] runtime/wasm: encapsulate continuation storage --- runtime/internal/runtime/fatal_default.go | 2 +- runtime/internal/runtime/fatal_wasm.go | 2 +- runtime/internal/runtime/proc_wasip1.go | 53 ++------- runtime/internal/runtime/proc_wasm.go | 90 +++------------ runtime/internal/runtime/runqueue_wasm.go | 19 ++++ runtime/internal/wasmcontext/context_js.go | 34 +++++- .../internal/wasmcontext/context_wasip1.go | 20 +++- runtime/internal/wasmcontext/doc.go | 5 +- runtime/internal/wasmcontext/storage.go | 61 ++++++++++ runtime/internal/wasmcontext/storage_test.go | 106 ++++++++++++++++++ 10 files changed, 259 insertions(+), 133 deletions(-) create mode 100644 runtime/internal/runtime/runqueue_wasm.go create mode 100644 runtime/internal/wasmcontext/storage.go create mode 100644 runtime/internal/wasmcontext/storage_test.go diff --git a/runtime/internal/runtime/fatal_default.go b/runtime/internal/runtime/fatal_default.go index 0046a9ec36..1ef2c713a5 100644 --- a/runtime/internal/runtime/fatal_default.go +++ b/runtime/internal/runtime/fatal_default.go @@ -1,4 +1,4 @@ -//go:build !llgo || !js || !wasm +//go:build !llgo || !wasm || (!js && !wasip1) package runtime diff --git a/runtime/internal/runtime/fatal_wasm.go b/runtime/internal/runtime/fatal_wasm.go index 3482947aab..ae71593fd7 100644 --- a/runtime/internal/runtime/fatal_wasm.go +++ b/runtime/internal/runtime/fatal_wasm.go @@ -1,4 +1,4 @@ -//go:build llgo && js && wasm +//go:build llgo && wasm && (js || wasip1) package runtime diff --git a/runtime/internal/runtime/proc_wasip1.go b/runtime/internal/runtime/proc_wasip1.go index 513caab15e..cfa88861e4 100644 --- a/runtime/internal/runtime/proc_wasip1.go +++ b/runtime/internal/runtime/proc_wasip1.go @@ -25,15 +25,10 @@ import ( "github.com/goplus/llgo/runtime/internal/wasmcontext" ) -const ( - defaultWasmGStackSize = 64 << 10 - defaultWasmAsyncifyStackSize = 64 << 10 -) - type runtimeContextPlatform struct { - context wasmcontext.Context - stack unsafe.Pointer - asyncifyStack unsafe.Pointer + context wasmcontext.Context + runqNext *g + runqQueued bool } var wasmSched struct { @@ -136,39 +131,15 @@ func newprocBackend(fn goroutineFunc, arg unsafe.Pointer, stackSize uintptr, cal } func initWasmContext(gp *g, entry wasmcontext.Entry, arg unsafe.Pointer, stackSize uintptr) { - if stackSize == 0 { - stackSize = defaultWasmGStackSize - } - stackSize = alignWasmStackSize(stackSize) - asyncifySize := uintptr(defaultWasmAsyncifyStackSize) - if stackSize > asyncifySize { - asyncifySize = stackSize - } - - platform := &gp.context.platform - platform.stack = allocWasmStack(stackSize) - platform.asyncifyStack = allocWasmStack(asyncifySize) - platform.context.Init( + if !gp.context.platform.context.Init( entry, arg, - platform.stack, stackSize, - platform.asyncifyStack, - asyncifySize, - ) -} - -func alignWasmStackSize(size uintptr) uintptr { - const alignment = uintptr(16) - return (size + alignment - 1) &^ (alignment - 1) -} - -func allocWasmStack(size uintptr) unsafe.Pointer { - stack := AllocRoot(size) - if stack == nil { + AllocRoot, + FreeRoot, + ) { panic("runtime: failed to allocate WebAssembly goroutine stack") } - return stack } func releaseWasmContext(gp *g) { @@ -176,15 +147,7 @@ func releaseWasmContext(gp *g) { return } ctx := gp.context - platform := &ctx.platform - if platform.stack != nil { - FreeRoot(platform.stack) - platform.stack = nil - } - if platform.asyncifyStack != nil { - FreeRoot(platform.asyncifyStack) - platform.asyncifyStack = nil - } + ctx.platform.context.Close(FreeRoot) freeRuntimeContext(ctx) } diff --git a/runtime/internal/runtime/proc_wasm.go b/runtime/internal/runtime/proc_wasm.go index 383f47a149..7d86bbce81 100644 --- a/runtime/internal/runtime/proc_wasm.go +++ b/runtime/internal/runtime/proc_wasm.go @@ -25,17 +25,10 @@ import ( "github.com/goplus/llgo/runtime/internal/wasmcontext" ) -const ( - defaultWasmGStackSize = 64 << 10 - defaultWasmAsyncifyStackSize = 64 << 10 -) - type runtimeContextPlatform struct { - context wasmcontext.Context - stack unsafe.Pointer - asyncifyStack unsafe.Pointer - runqNext *g - runqQueued bool + context wasmcontext.Context + runqNext *g + runqQueued bool } var wasmSched struct { @@ -46,22 +39,6 @@ var wasmSched struct { started bool } -func (gp *g) RunqueueNext() *g { - return gp.context.platform.runqNext -} - -func (gp *g) SetRunqueueNext(next *g) { - gp.context.platform.runqNext = next -} - -func (gp *g) RunqueueQueued() bool { - return gp.context.platform.runqQueued -} - -func (gp *g) SetRunqueueQueued(queued bool) { - gp.context.platform.runqQueued = queued -} - func initRuntimeContext(ctx *runtimeContext, callergp *g, status uint32) *g { gp := initG(ctx, callergp, status) if status == _Grunning { @@ -101,57 +78,24 @@ func newprocBackend(fn goroutineFunc, arg unsafe.Pointer, stackSize uintptr, cal } func initWasmFiber(gp *g, stackSize uintptr) bool { - if stackSize == 0 { - stackSize = defaultWasmGStackSize - } - stackSize = alignWasmStackSize(stackSize) - asyncifySize := uintptr(defaultWasmAsyncifyStackSize) - if stackSize > asyncifySize { - asyncifySize = stackSize - } - platform := &gp.context.platform - platform.stack = AllocRoot(stackSize) - if platform.stack == nil { - return false - } - platform.asyncifyStack = AllocRoot(asyncifySize) - if platform.asyncifyStack == nil { - FreeRoot(platform.stack) - platform.stack = nil - return false - } - platform.context.Init( + return platform.context.Init( wasmcontext.Entry(wasmGStart), unsafe.Pointer(gp), - platform.stack, stackSize, - platform.asyncifyStack, - asyncifySize, + AllocRoot, + FreeRoot, ) - return true -} - -func alignWasmStackSize(size uintptr) uintptr { - const alignment = uintptr(16) - return (size + alignment - 1) &^ (alignment - 1) -} - -func allocWasmStack(size uintptr) unsafe.Pointer { - stack := AllocRoot(size) - if stack == nil { - panic("runtime: failed to allocate WebAssembly goroutine stack") - } - return stack } func ensureCurrentWasmFiber(gp *g) { - platform := &gp.context.platform - if platform.asyncifyStack != nil { + context := &gp.context.platform.context + if context.Ready() { return } - platform.asyncifyStack = allocWasmStack(defaultWasmAsyncifyStackSize) - platform.context.InitCurrent(platform.asyncifyStack, defaultWasmAsyncifyStackSize) + if !context.InitCurrent(AllocRoot) { + panic("runtime: failed to allocate WebAssembly goroutine stack") + } } func wasmGStart(arg unsafe.Pointer) { @@ -211,7 +155,7 @@ func resumeWasmG(old, next *g) { return } ensureCurrentWasmFiber(old) - if next.context.platform.asyncifyStack == nil { + if !next.context.platform.context.Ready() { fatal("runtime: uninitialized WebAssembly goroutine context") return } @@ -266,15 +210,7 @@ func reapRetiredWasmG() { return } wasmSched.retired = nil - platform := &ctx.platform - if platform.stack != nil { - FreeRoot(platform.stack) - platform.stack = nil - } - if platform.asyncifyStack != nil { - FreeRoot(platform.asyncifyStack) - platform.asyncifyStack = nil - } + ctx.platform.context.Close(FreeRoot) freeRuntimeContext(ctx) } diff --git a/runtime/internal/runtime/runqueue_wasm.go b/runtime/internal/runtime/runqueue_wasm.go new file mode 100644 index 0000000000..c5e5e0e7f4 --- /dev/null +++ b/runtime/internal/runtime/runqueue_wasm.go @@ -0,0 +1,19 @@ +//go:build llgo && wasm && (js || (wasip1 && !llgo.wasi_threads)) + +package runtime + +func (gp *g) RunqueueNext() *g { + return gp.context.platform.runqNext +} + +func (gp *g) SetRunqueueNext(next *g) { + gp.context.platform.runqNext = next +} + +func (gp *g) RunqueueQueued() bool { + return gp.context.platform.runqQueued +} + +func (gp *g) SetRunqueueQueued(queued bool) { + gp.context.platform.runqQueued = queued +} diff --git a/runtime/internal/wasmcontext/context_js.go b/runtime/internal/wasmcontext/context_js.go index 4550c38320..56e267c420 100644 --- a/runtime/internal/wasmcontext/context_js.go +++ b/runtime/internal/wasmcontext/context_js.go @@ -28,10 +28,18 @@ type Entry = emscripten.FiberEntry // Context wraps the Emscripten Fiber ABI used by JavaScript hosts. type Context struct { - fiber emscripten.Fiber + fiber emscripten.Fiber + stack unsafe.Pointer + asyncifyStack unsafe.Pointer } -func (ctx *Context) Init(entry Entry, arg, stack unsafe.Pointer, stackSize uintptr, asyncifyStack unsafe.Pointer, asyncifyStackSize uintptr) { +func (ctx *Context) Init(entry Entry, arg unsafe.Pointer, stackSize uintptr, alloc func(uintptr) unsafe.Pointer, free func(unsafe.Pointer)) bool { + stack, stackSize, asyncifyStack, asyncifySize, ok := allocStorage(stackSize, alloc, free) + if !ok { + return false + } + ctx.stack = stack + ctx.asyncifyStack = asyncifyStack emscripten.FiberInit( &ctx.fiber, entry, @@ -39,12 +47,28 @@ func (ctx *Context) Init(entry Entry, arg, stack unsafe.Pointer, stackSize uintp stack, stackSize, asyncifyStack, - asyncifyStackSize, + asyncifySize, ) + return true } -func (ctx *Context) InitCurrent(asyncifyStack unsafe.Pointer, asyncifyStackSize uintptr) { - emscripten.FiberInitCurrent(&ctx.fiber, asyncifyStack, asyncifyStackSize) +func (ctx *Context) InitCurrent(alloc func(uintptr) unsafe.Pointer) bool { + asyncifyStack := alloc(defaultAsyncifyStackSize) + if asyncifyStack == nil { + return false + } + ctx.asyncifyStack = asyncifyStack + emscripten.FiberInitCurrent(&ctx.fiber, asyncifyStack, defaultAsyncifyStackSize) + return true +} + +func (ctx *Context) Ready() bool { + return ctx.asyncifyStack != nil +} + +func (ctx *Context) Close(free func(unsafe.Pointer)) { + freeStorage(ctx.stack, ctx.asyncifyStack, free) + *ctx = Context{} } func (ctx *Context) Swap(next *Context) { diff --git a/runtime/internal/wasmcontext/context_wasip1.go b/runtime/internal/wasmcontext/context_wasip1.go index 63177044cc..837b42981b 100644 --- a/runtime/internal/wasmcontext/context_wasip1.go +++ b/runtime/internal/wasmcontext/context_wasip1.go @@ -36,15 +36,31 @@ type Context struct { asyncifyEnd unsafe.Pointer stackPointer unsafe.Pointer launched bool + stack unsafe.Pointer } -func (ctx *Context) Init(entry Entry, arg, stack unsafe.Pointer, stackSize uintptr, asyncifyStack unsafe.Pointer, asyncifyStackSize uintptr) { +func (ctx *Context) Init(entry Entry, arg unsafe.Pointer, stackSize uintptr, alloc func(uintptr) unsafe.Pointer, free func(unsafe.Pointer)) bool { + stack, stackSize, asyncifyStack, asyncifySize, ok := allocStorage(stackSize, alloc, free) + if !ok { + return false + } ctx.entry = c.Func(entry) ctx.arg = arg ctx.asyncifyStack = asyncifyStack - ctx.asyncifyEnd = unsafe.Add(asyncifyStack, asyncifyStackSize) + ctx.asyncifyEnd = unsafe.Add(asyncifyStack, asyncifySize) ctx.stackPointer = unsafe.Add(stack, stackSize) ctx.launched = false + ctx.stack = stack + return true +} + +func (ctx *Context) Ready() bool { + return ctx.asyncifyStack != nil +} + +func (ctx *Context) Close(free func(unsafe.Pointer)) { + freeStorage(ctx.stack, ctx.asyncifyStack, free) + *ctx = Context{} } func (ctx *Context) Resume() { diff --git a/runtime/internal/wasmcontext/doc.go b/runtime/internal/wasmcontext/doc.go index 688f6da9b7..1f0446f7b0 100644 --- a/runtime/internal/wasmcontext/doc.go +++ b/runtime/internal/wasmcontext/doc.go @@ -14,6 +14,7 @@ * limitations under the License. */ -// Package wasmcontext provides suspended execution contexts for WebAssembly -// runtime schedulers. +// Package wasmcontext owns suspended WebAssembly execution contexts and their +// backend-specific storage. Runtime schedulers provide root-aware allocation +// callbacks during context creation and do not inspect the resulting buffers. package wasmcontext diff --git a/runtime/internal/wasmcontext/storage.go b/runtime/internal/wasmcontext/storage.go new file mode 100644 index 0000000000..6b5010dbf0 --- /dev/null +++ b/runtime/internal/wasmcontext/storage.go @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package wasmcontext + +import "unsafe" + +const ( + defaultStackSize = uintptr(64 << 10) + defaultAsyncifyStackSize = uintptr(64 << 10) + stackAlignment = uintptr(16) +) + +func allocStorage(stackSize uintptr, alloc func(uintptr) unsafe.Pointer, free func(unsafe.Pointer)) (stack unsafe.Pointer, normalizedStackSize uintptr, asyncifyStack unsafe.Pointer, asyncifySize uintptr, ok bool) { + if stackSize == 0 { + stackSize = defaultStackSize + } + stackSize = alignStackSize(stackSize) + asyncifySize = defaultAsyncifyStackSize + if stackSize > asyncifySize { + asyncifySize = stackSize + } + + stack = alloc(stackSize) + if stack == nil { + return + } + asyncifyStack = alloc(asyncifySize) + if asyncifyStack == nil { + free(stack) + stack = nil + return + } + return stack, stackSize, asyncifyStack, asyncifySize, true +} + +func freeStorage(stack, asyncifyStack unsafe.Pointer, free func(unsafe.Pointer)) { + if stack != nil { + free(stack) + } + if asyncifyStack != nil { + free(asyncifyStack) + } +} + +func alignStackSize(size uintptr) uintptr { + return (size + stackAlignment - 1) &^ (stackAlignment - 1) +} diff --git a/runtime/internal/wasmcontext/storage_test.go b/runtime/internal/wasmcontext/storage_test.go new file mode 100644 index 0000000000..f938bb0574 --- /dev/null +++ b/runtime/internal/wasmcontext/storage_test.go @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package wasmcontext + +import ( + "testing" + "unsafe" +) + +func TestStorageLifecycle(t *testing.T) { + var allocated []uintptr + var freed []unsafe.Pointer + buffers := make([][]byte, 0, 2) + alloc := func(size uintptr) unsafe.Pointer { + allocated = append(allocated, size) + buf := make([]byte, size) + buffers = append(buffers, buf) + return unsafe.Pointer(&buf[0]) + } + free := func(ptr unsafe.Pointer) { + freed = append(freed, ptr) + } + + stack, stackSize, asyncify, asyncifySize, ok := allocStorage(defaultStackSize+1, alloc, free) + if !ok { + t.Fatal("init failed") + } + wantSize := defaultStackSize + stackAlignment + if len(allocated) != 2 || allocated[0] != wantSize || allocated[1] != wantSize { + t.Fatalf("allocated sizes = %v, want [%d %d]", allocated, wantSize, wantSize) + } + if stackSize != wantSize || asyncifySize != wantSize { + t.Fatalf("returned sizes = %d/%d, want %d/%d", stackSize, asyncifySize, wantSize, wantSize) + } + + freeStorage(stack, asyncify, free) + if len(freed) != 2 || freed[0] != stack || freed[1] != asyncify { + t.Fatalf("freed pointers = %v, want [%p %p]", freed, stack, asyncify) + } +} + +func TestStorageInitFailure(t *testing.T) { + buf := make([]byte, defaultStackSize) + stack := unsafe.Pointer(&buf[0]) + for _, failAt := range []int{1, 2} { + allocations := 0 + alloc := func(uintptr) unsafe.Pointer { + allocations++ + if allocations == failAt { + return nil + } + return stack + } + var freed unsafe.Pointer + + stackResult, _, asyncifyResult, _, ok := allocStorage(0, alloc, func(ptr unsafe.Pointer) { freed = ptr }) + if ok { + t.Fatalf("allocation %d failure succeeded", failAt) + } + wantFreed := unsafe.Pointer(nil) + if failAt == 2 { + wantFreed = stack + } + if freed != wantFreed { + t.Fatalf("allocation %d freed pointer = %p, want %p", failAt, freed, wantFreed) + } + if stackResult != nil || asyncifyResult != nil { + t.Fatalf("allocation %d failure returned storage", failAt) + } + } +} + +func TestStorageDefaultSize(t *testing.T) { + var sizes []uintptr + buffers := make([][]byte, 0, 2) + stack, stackSize, asyncify, asyncifySize, ok := allocStorage(0, func(size uintptr) unsafe.Pointer { + sizes = append(sizes, size) + buf := make([]byte, size) + buffers = append(buffers, buf) + return unsafe.Pointer(&buf[0]) + }, func(unsafe.Pointer) {}) + if !ok { + t.Fatal("init failed") + } + if stackSize != defaultStackSize || asyncifySize != defaultAsyncifyStackSize { + t.Fatalf("default sizes = %d/%d", stackSize, asyncifySize) + } + if len(sizes) != 2 || sizes[0] != defaultStackSize || sizes[1] != defaultAsyncifyStackSize { + t.Fatalf("requested sizes = %v", sizes) + } + freeStorage(stack, asyncify, func(unsafe.Pointer) {}) +} From 31abe5baa487ccff857b3feaf56180d32ccda9d8 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Thu, 30 Jul 2026 10:08:44 +0800 Subject: [PATCH 16/18] runtime/wasm: preserve explicit WASI thread fatal behavior --- runtime/internal/runtime/fatal_default.go | 2 +- runtime/internal/runtime/fatal_wasm.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/internal/runtime/fatal_default.go b/runtime/internal/runtime/fatal_default.go index 1ef2c713a5..32b9c9dc96 100644 --- a/runtime/internal/runtime/fatal_default.go +++ b/runtime/internal/runtime/fatal_default.go @@ -1,4 +1,4 @@ -//go:build !llgo || !wasm || (!js && !wasip1) +//go:build !llgo || !wasm || (!js && !wasip1) || (wasip1 && llgo.wasi_threads) package runtime diff --git a/runtime/internal/runtime/fatal_wasm.go b/runtime/internal/runtime/fatal_wasm.go index ae71593fd7..1455d0ce08 100644 --- a/runtime/internal/runtime/fatal_wasm.go +++ b/runtime/internal/runtime/fatal_wasm.go @@ -1,4 +1,4 @@ -//go:build llgo && wasm && (js || wasip1) +//go:build llgo && wasm && (js || (wasip1 && !llgo.wasi_threads)) package runtime From df948c82d5845b009929faddecd90b9b43671544 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Mon, 3 Aug 2026 13:42:31 +0800 Subject: [PATCH 17/18] runtime/wasm: clean up failed WASI context allocation --- runtime/internal/runtime/proc_wasip1.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/runtime/internal/runtime/proc_wasip1.go b/runtime/internal/runtime/proc_wasip1.go index cfa88861e4..d0c3d85458 100644 --- a/runtime/internal/runtime/proc_wasip1.go +++ b/runtime/internal/runtime/proc_wasip1.go @@ -75,7 +75,9 @@ func RunWasmMain() { fatal("runtime: invalid WebAssembly main goroutine") return } - initWasmContext(gp, wasmcontext.Entry(wasmMainTask), nil, 0) + if !initWasmContext(gp, wasmcontext.Entry(wasmMainTask), nil, 0) { + panic("runtime: failed to allocate WebAssembly goroutine stack") + } for { runWasmContext(gp) @@ -124,22 +126,24 @@ func releaseWasmOwnership(gp *g) { func newprocBackend(fn goroutineFunc, arg unsafe.Pointer, stackSize uintptr, callergp *g) { gp := newproc1(fn, arg, callergp) - initWasmContext(gp, wasmcontext.Entry(wasmGStart), unsafe.Pointer(gp), stackSize) + if !initWasmContext(gp, wasmcontext.Entry(wasmGStart), unsafe.Pointer(gp), stackSize) { + FreeRoot(arg) + freeRuntimeContext(gp.context) + panic("runtime: failed to allocate WebAssembly goroutine stack") + } if !wasmSched.runq.Push(gp) { fatal("runtime: invalid run queue insertion") } } -func initWasmContext(gp *g, entry wasmcontext.Entry, arg unsafe.Pointer, stackSize uintptr) { - if !gp.context.platform.context.Init( +func initWasmContext(gp *g, entry wasmcontext.Entry, arg unsafe.Pointer, stackSize uintptr) bool { + return gp.context.platform.context.Init( entry, arg, stackSize, AllocRoot, FreeRoot, - ) { - panic("runtime: failed to allocate WebAssembly goroutine stack") - } + ) } func releaseWasmContext(gp *g) { From 51ad21aee9d0c1587e8edfe7dfa1ee28429a7c36 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Sun, 9 Aug 2026 23:09:59 +0800 Subject: [PATCH 18/18] runtime/wasm: align WASI scheduler lifecycle --- .github/workflows/llgo.yml | 13 +++++++++++++ internal/build/testdata/wasm-scheduler/abi.c | 4 ++++ internal/build/testdata/wasm-scheduler/abi.go | 3 +++ internal/build/testdata/wasm-scheduler/main.go | 11 +++++++++++ runtime/internal/runtime/proc_wasip1.go | 18 +++++++++--------- 5 files changed, 40 insertions(+), 9 deletions(-) diff --git a/.github/workflows/llgo.yml b/.github/workflows/llgo.yml index b9a8a500d2..7c58e5fb79 100644 --- a/.github/workflows/llgo.yml +++ b/.github/workflows/llgo.yml @@ -420,6 +420,12 @@ jobs: return 1 fi grep -Fq "fatal error: all goroutines are asleep - deadlock!" <<<"$output" + if output=$(node --input-type=module -e "import Module from '$module'; await Module({preRun: [module => { module.ENV.LLGO_WASM_SCHEDULER_MAIN_GOEXIT = '1'; }]});" 2>&1); then + echo "main Goexit scheduler fixture unexpectedly succeeded" + return 1 + fi + grep -Fq "WORKER_RETURNING" <<<"$output" + grep -Fq "fatal error: no goroutines (main called runtime.Goexit) - deadlock!" <<<"$output" } run_wasi_scheduler() { @@ -434,6 +440,13 @@ jobs: return 1 fi grep -Fq "fatal error: all goroutines are asleep - deadlock!" <<<"$output" + if output=$(wasmtime run -W exceptions=y \ + --env LLGO_WASM_SCHEDULER_MAIN_GOEXIT=1 "$module" 2>&1); then + echo "main Goexit scheduler fixture unexpectedly succeeded" + return 1 + fi + grep -Fq "WORKER_RETURNING" <<<"$output" + grep -Fq "fatal error: no goroutines (main called runtime.Goexit) - deadlock!" <<<"$output" } GOOS=js GOARCH=wasm llgo build -o "$RUNNER_TEMP/runtime-js.wasm" ./internal/build/testdata/wasm-runtime diff --git a/internal/build/testdata/wasm-scheduler/abi.c b/internal/build/testdata/wasm-scheduler/abi.c index b648edf3ec..e57c636606 100644 --- a/internal/build/testdata/wasm-scheduler/abi.c +++ b/internal/build/testdata/wasm-scheduler/abi.c @@ -8,3 +8,7 @@ size_t llgo_test_sizeof_long(void) { int llgo_test_scheduler_deadlock(void) { return getenv("LLGO_WASM_SCHEDULER_DEADLOCK") != NULL; } + +int llgo_test_scheduler_main_goexit(void) { + return getenv("LLGO_WASM_SCHEDULER_MAIN_GOEXIT") != NULL; +} diff --git a/internal/build/testdata/wasm-scheduler/abi.go b/internal/build/testdata/wasm-scheduler/abi.go index caa04596fc..a267033ec3 100644 --- a/internal/build/testdata/wasm-scheduler/abi.go +++ b/internal/build/testdata/wasm-scheduler/abi.go @@ -9,3 +9,6 @@ func cLongSize() uintptr //go:linkname schedulerDeadlockMode C.llgo_test_scheduler_deadlock func schedulerDeadlockMode() int32 + +//go:linkname schedulerMainGoexitMode C.llgo_test_scheduler_main_goexit +func schedulerMainGoexitMode() int32 diff --git a/internal/build/testdata/wasm-scheduler/main.go b/internal/build/testdata/wasm-scheduler/main.go index 399a5b39c5..5388210a2d 100644 --- a/internal/build/testdata/wasm-scheduler/main.go +++ b/internal/build/testdata/wasm-scheduler/main.go @@ -60,6 +60,10 @@ func checkCurrentG() { func main() { checkWasmModel() + if schedulerMainGoexitMode() != 0 { + testMainGoexit() + return + } if schedulerDeadlockMode() != 0 { testParkedMainDeadlock() return @@ -139,6 +143,13 @@ func main() { println("wasm scheduler ok") } +func testMainGoexit() { + go func() { + println("WORKER_RETURNING") + }() + runtime.Goexit() +} + func testGoroutineLifecycle() { const count = 5000 for i := 1; i <= count; i++ { diff --git a/runtime/internal/runtime/proc_wasip1.go b/runtime/internal/runtime/proc_wasip1.go index d0c3d85458..22fa420d4b 100644 --- a/runtime/internal/runtime/proc_wasip1.go +++ b/runtime/internal/runtime/proc_wasip1.go @@ -32,11 +32,10 @@ type runtimeContextPlatform struct { } var wasmSched struct { - m m - p p - runq runqueue.Queue[*g] - started bool - mainExited bool + m m + p p + runq runqueue.Queue[*g] + started bool } func initRuntimeContext(ctx *runtimeContext, callergp *g, status uint32) *g { @@ -84,6 +83,7 @@ func RunWasmMain() { status := readgstatus(gp) if gp.isMain && status == _Grunning { casgstatus(gp, _Grunning, _Gdead) + releaseG() releaseWasmContext(gp) return } @@ -94,7 +94,8 @@ func RunWasmMain() { gp = wasmSched.runq.Pop() if gp == nil { - if wasmSched.mainExited { + _, mainExited := gStateForTesting() + if mainExited { fatal("no goroutines (main called runtime.Goexit) - deadlock!") } else { fatal("all goroutines are asleep - deadlock!") @@ -127,6 +128,7 @@ func releaseWasmOwnership(gp *g) { func newprocBackend(fn goroutineFunc, arg unsafe.Pointer, stackSize uintptr, callergp *g) { gp := newproc1(fn, arg, callergp) if !initWasmContext(gp, wasmcontext.Entry(wasmGStart), unsafe.Pointer(gp), stackSize) { + releaseG() FreeRoot(arg) freeRuntimeContext(gp.context) panic("runtime: failed to allocate WebAssembly goroutine stack") @@ -198,9 +200,7 @@ func goready(gp *g) { func goexitBackend(gp *g) { casgstatus(gp, _Grunning, _Gdead) - if gp.isMain { - wasmSched.mainExited = true - } + releaseGAndCheckDeadlock() gp.context.platform.context.Suspend() fatal("runtime: resumed dead WebAssembly goroutine") }