Skip to content

compileopts: forward -stack-size to wasm-ld when -scheduler=none - #6103

Open
teamchong wants to merge 1 commit into
tinygo-org:devfrom
teamchong:fix/wasm-ld-stack-size
Open

teamchong wants to merge 1 commit into
tinygo-org:devfrom
teamchong:fix/wasm-ld-stack-size

Conversation

@teamchong

Copy link
Copy Markdown

On wasm and wasip1, -stack-size sizes goroutine stacks when a scheduler is active, but was ignored when -scheduler=none was set (leaving wasm-ld at its 64 KiB default linear stack).

Forward -stack-size as -z stack-size=<bytes> to wasm-ld when -scheduler=none is set.

package main

func recurse(n int) int {
	var buf [1024]byte
	if n == 0 {
		return int(buf[0])
	}
	return int(buf[n&1023]) + recurse(n-1)
}

func main() {
	println(recurse(200))
}

Before (dev):

$ tinygo run -target=wasip1 -scheduler=none -stack-size=1MB repro.go
wasm trap: out of bounds memory access (64 KiB linear stack overflow)

After (this PR):

$ tinygo run -target=wasip1 -scheduler=none -stack-size=1MB repro.go
0

When -scheduler=none is used on wasm-ld targets, there are no goroutine stacks and execution uses the linear shadow stack directly. Forward -stack-size as -z stack-size=<bytes> to wasm-ld.
Comment thread compileopts/config.go
if c.Target.LinkerScript != "" {
ldflags = append(ldflags, "-T", c.Target.LinkerScript)
}
if c.Target.Linker == "wasm-ld" && c.Scheduler() == "none" && c.Options.StackSize != 0 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to gate this on the scheduler flag or can the linker flag be forwarded unconditionally?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with asyncify, goroutine stacks (including main) are allocated on the heap using -stack-size, and wasm-ld's linear stack is only used by the scheduler loop — so passing -z stack-size to wasm-ld would reserve -stack-size a second time in static memory

with -scheduler=none, the whole program runs on wasm-ld's stack and -stack-size is otherwise ignored

happy to make it unconditional if you prefer though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants