Repository navigation
fix(wasi): size initial memory from static data and heap budget #2541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cpunion
merged 4 commits into
xgo-dev:main
from
cpunion:codex/fix-wasi-initial-heap-20260909
Sep 22, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2e80ae2
fix(wasi): size initial memory from static data and heap budget
cpunion f4e3742
refactor(build): inspect WASI defaults with the active linker command
cpunion 1a2614c
fix(wasm): bootstrap empty heaps and honor response-file policy
cpunion ea838b5
test(wasm): name coupled memory constants
cpunion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * 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 ( | ||
| "strings" | ||
|
|
||
| "github.com/xgo-dev/llgo/internal/clang" | ||
| ) | ||
|
|
||
| // Retain the previous 64 MiB total minus 10 MiB process-stack heap budget, | ||
| // without capping static data at the remainder of that total. wasm-ld adds this | ||
| // heap reservation after static data and the process stack, so a normal module | ||
| // initially has 64 MiB plus its static data (rounded to Wasm pages). This does | ||
| // not change the process stack or impose a maximum linear-memory size. | ||
| // The 10 MiB value matches -z stack-size=10485760 in crosscompile.go. | ||
| const defaultWASIHeapFlag = "-Wl,--initial-heap=56623104" | ||
|
cpunion marked this conversation as resolved.
|
||
|
|
||
| func defaultWASIHeapArgs(ctx *context, linker *clang.Cmd, args []string) []string { | ||
| if ctx == nil || ctx.buildConf == nil || ctx.buildConf.Goos != "wasip1" || ctx.buildConf.Goarch != "wasm" { | ||
| return nil | ||
| } | ||
| // Wait until all flags, including package link directives and the linker | ||
| // command prefix, are available. An explicit initial-memory or initial-heap | ||
| // option owns the complete policy; adding a default heap alongside an exact | ||
| // initial-memory size could make an otherwise valid user setting too small. | ||
| // A user response file also owns the policy: it may contain either option, | ||
| // including in a nested file. Leave its contents and quoting to the driver. | ||
| // The driver's automatic response-file creation happens after inspection. | ||
| // Reuse the command that will perform the link. Its MSVC library resolution | ||
| // is a no-op for WASI targets, so inspecting these flags performs no file I/O. | ||
| for _, arg := range linker.LinkArguments(args...) { | ||
| options := []string{arg} | ||
| if strings.HasPrefix(arg, "-Wl,") { | ||
| options = strings.Split(strings.TrimPrefix(arg, "-Wl,"), ",") | ||
| } | ||
| for _, option := range options { | ||
| if strings.HasPrefix(option, "@") || | ||
| option == "--initial-memory" || strings.HasPrefix(option, "--initial-memory=") || | ||
| option == "--initial-heap" || strings.HasPrefix(option, "--initial-heap=") { | ||
| return nil | ||
| } | ||
| } | ||
| } | ||
| return []string{defaultWASIHeapFlag} | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| //go:build !llgo | ||
|
|
||
| package build | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "slices" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/xgo-dev/llgo/internal/crosscompile" | ||
| ) | ||
|
|
||
| func TestWASIHeapReachesLinker(t *testing.T) { | ||
| t.Setenv("CCFLAGS", "") | ||
| t.Setenv("LDFLAGS", "") | ||
| t.Setenv("LLGO_TEST_LINKER_HELPER", "write") | ||
| for _, test := range []struct { | ||
| name string | ||
| goos string | ||
| config []string | ||
| args []string | ||
| wantHeap bool | ||
| }{ | ||
| {name: "single worker default", goos: "wasip1", wantHeap: true}, | ||
| {name: "explicit initial memory", goos: "wasip1", args: []string{"-Wl,--initial-memory=33554432"}}, | ||
| {name: "explicit initial heap", goos: "wasip1", args: []string{"-Wl,--initial-heap=1048576"}}, | ||
| {name: "driver response", goos: "wasip1", args: []string{"@user flags.rsp"}}, | ||
| {name: "linker response", goos: "wasip1", args: []string{"-Wl,@user flags.rsp"}}, | ||
| {name: "configured response", goos: "wasip1", config: []string{"-Xlinker", "@user flags.rsp"}}, | ||
| {name: "shared memory contract", goos: "wasip1", config: []string{"-Wl,--initial-memory=67108864", "-Wl,--import-memory"}}, | ||
| {name: "emscripten unchanged", goos: "js"}, | ||
| } { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| dir := t.TempDir() | ||
| argsFile := filepath.Join(dir, "link-args.txt") | ||
| t.Setenv("LINK_ARGS_FILE", argsFile) | ||
| ctx := &context{ | ||
| buildConf: &Config{ | ||
| Goos: test.goos, Goarch: "wasm", BuildMode: BuildModeExe, | ||
| LinkOptions: LinkOptions{DWARF: DWARFOmit}, | ||
| }, | ||
| crossCompile: crosscompile.Export{Linker: os.Args[0], LDFLAGS: test.config}, | ||
| } | ||
| if err := linkObjFiles(ctx, filepath.Join(dir, "app.wasm"), nil, test.args, false); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| data, err := os.ReadFile(argsFile) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| args := strings.Split(strings.TrimSpace(string(data)), "\n") | ||
| if got := slices.Contains(args, defaultWASIHeapFlag); got != test.wantHeap { | ||
| t.Fatalf("default heap in linker arguments = %v, want %v: %q", got, test.wantHeap, args) | ||
| } | ||
| for _, flag := range slices.Concat(test.config, test.args) { | ||
| if !slices.Contains(args, flag) { | ||
| t.Errorf("explicit linker option %q was lost: %q", flag, args) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestDefaultWASIHeapArgs(t *testing.T) { | ||
| for _, test := range []struct { | ||
| name string | ||
| goos string | ||
| goarch string | ||
| args []string | ||
| config []string | ||
| prefix []string | ||
| ccflags string | ||
| ldflags string | ||
| wantHeap bool | ||
| }{ | ||
| {name: "default", goos: "wasip1", goarch: "wasm", wantHeap: true}, | ||
| {name: "named WASI flags", goos: "wasip1", goarch: "wasm", config: []string{"-target", "wasm32-unknown-wasip1", "-Wl,--stack-first"}, wantHeap: true}, | ||
| {name: "native", goos: "linux", goarch: "amd64"}, | ||
| {name: "non-wasm architecture", goos: "wasip1", goarch: "amd64"}, | ||
| {name: "emscripten", goos: "js", goarch: "wasm"}, | ||
| {name: "package memory", goos: "wasip1", goarch: "wasm", args: []string{"-Wl,--initial-memory=33554432"}}, | ||
| {name: "package heap", goos: "wasip1", goarch: "wasm", args: []string{"-Wl,--initial-heap=1048576"}}, | ||
| {name: "separate driver option", goos: "wasip1", goarch: "wasm", args: []string{"-Wl,--initial-memory,33554432"}}, | ||
| {name: "xlinker separate", goos: "wasip1", goarch: "wasm", args: []string{"-Xlinker", "--initial-heap", "-Xlinker", "0"}}, | ||
| {name: "xlinker equals", goos: "wasip1", goarch: "wasm", args: []string{"-Xlinker", "--initial-memory=33554432"}}, | ||
| {name: "direct linker option", goos: "wasip1", goarch: "wasm", args: []string{"--initial-memory", "33554432"}}, | ||
| {name: "config or extldflags", goos: "wasip1", goarch: "wasm", config: []string{"-Wl,--initial-memory=33554432"}}, | ||
| {name: "linker prefix", goos: "wasip1", goarch: "wasm", prefix: []string{"-Wl,--initial-heap=1048576"}}, | ||
| {name: "CCFLAGS", goos: "wasip1", goarch: "wasm", ccflags: "-Wl,--initial-memory=33554432"}, | ||
| {name: "LDFLAGS", goos: "wasip1", goarch: "wasm", ldflags: "-Xlinker --initial-heap=1048576"}, | ||
| {name: "maximum unchanged", goos: "wasip1", goarch: "wasm", args: []string{"-Wl,--max-memory=268435456"}, wantHeap: true}, | ||
| {name: "unrelated names", goos: "wasip1", goarch: "wasm", args: []string{"data-initial-memory.o", "objects@user.o", "objects,@user.o", "-Wl,-Map,initial-heap.map"}, wantHeap: true}, | ||
| {name: "driver response", goos: "wasip1", goarch: "wasm", args: []string{"@user flags.rsp"}}, | ||
| {name: "linker response", goos: "wasip1", goarch: "wasm", args: []string{"-Wl,@user flags.rsp"}}, | ||
| {name: "combined linker response", goos: "wasip1", goarch: "wasm", args: []string{"-Wl,--export=main,@objects.rsp"}}, | ||
| {name: "xlinker response", goos: "wasip1", goarch: "wasm", args: []string{"-Xlinker", "@user flags.rsp"}}, | ||
| {name: "config response", goos: "wasip1", goarch: "wasm", config: []string{"@user flags.rsp"}}, | ||
| {name: "linker prefix response", goos: "wasip1", goarch: "wasm", prefix: []string{"@user flags.rsp"}}, | ||
| {name: "CCFLAGS response", goos: "wasip1", goarch: "wasm", ccflags: "-O2 -Wl,@user flags.rsp"}, | ||
| {name: "LDFLAGS response", goos: "wasip1", goarch: "wasm", ldflags: "-O2 -Wl,@user flags.rsp"}, | ||
| } { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| t.Setenv("CCFLAGS", test.ccflags) | ||
| t.Setenv("LDFLAGS", test.ldflags) | ||
| ctx := &context{ | ||
| buildConf: &Config{Goos: test.goos, Goarch: test.goarch}, | ||
| crossCompile: crosscompile.Export{ | ||
| Linker: "clang", LinkerArgs: test.prefix, LDFLAGS: test.config, | ||
| }, | ||
| } | ||
| linker := ctx.linker() | ||
| before := linker.LinkArguments(test.args...) | ||
| got := defaultWASIHeapArgs(ctx, linker, test.args) | ||
| var want []string | ||
| if test.wantHeap { | ||
| want = []string{defaultWASIHeapFlag} | ||
| } | ||
| if !slices.Equal(got, want) { | ||
| t.Fatalf("flags = %q, want %q", got, want) | ||
| } | ||
| if after := linker.LinkArguments(test.args...); !slices.Equal(before, after) { | ||
| t.Fatalf("explicit arguments changed: before %q, after %q", before, after) | ||
| } | ||
| }) | ||
| } | ||
| if got := defaultWASIHeapArgs(nil, nil, nil); len(got) != 0 { | ||
| t.Fatalf("nil context: %q", got) | ||
| } | ||
| if got := defaultWASIHeapArgs(&context{}, nil, nil); len(got) != 0 { | ||
| t.Fatalf("missing config: %q", got) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.