Skip to content

Commit 17bb1fe

Browse files
committed
feature: add buildmode=wasi-legacy to support existing base of users who expected the
older behavior for wasi modules to not return an exit code as if they were reactors. See #4726 for some details on what this is intended to address. Signed-off-by: deadprogram <[email protected]>
1 parent f4fd79f commit 17bb1fe

File tree

6 files changed

+35
-2
lines changed

6 files changed

+35
-2
lines changed

Diff for: builder/build.go

+10
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,16 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
672672
ldflags = append(ldflags, "--no-entry")
673673
}
674674

675+
if config.Options.BuildMode == "wasi-legacy" {
676+
if !strings.HasPrefix(config.Triple(), "wasm32-") {
677+
return result, fmt.Errorf("buildmode wasi-legacy is only supported on wasm")
678+
}
679+
680+
if config.Options.Scheduler != "none" {
681+
return result, fmt.Errorf("buildmode wasi-legacy only supports scheduler=none")
682+
}
683+
}
684+
675685
// Add compiler-rt dependency if needed. Usually this is a simple load from
676686
// a cache.
677687
if config.Target.RTLib == "compiler-rt" {

Diff for: compileopts/options.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
var (
11-
validBuildModeOptions = []string{"default", "c-shared"}
11+
validBuildModeOptions = []string{"default", "c-shared", "wasi-legacy"}
1212
validGCOptions = []string{"none", "leaking", "conservative", "custom", "precise"}
1313
validSchedulerOptions = []string{"none", "tasks", "asyncify"}
1414
validSerialOptions = []string{"none", "uart", "usb", "rtt"}

Diff for: compiler/symbol.go

+5
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@ func (c *compilerContext) getFunctionInfo(f *ssa.Function) functionInfo {
283283
info.wasmName = "_start"
284284
info.exported = true
285285
}
286+
if info.linkName == "runtime.wasmEntryLegacy" && c.BuildMode == "wasi-legacy" {
287+
info.linkName = "_start"
288+
info.wasmName = "_start"
289+
info.exported = true
290+
}
286291

287292
// Check for //go: pragmas, which may change the link name (among others).
288293
c.parsePragmas(&info, f)

Diff for: main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1501,7 +1501,7 @@ func main() {
15011501
var tags buildutil.TagsFlag
15021502
flag.Var(&tags, "tags", "a space-separated list of extra build tags")
15031503
target := flag.String("target", "", "chip/board name or JSON target specification file")
1504-
buildMode := flag.String("buildmode", "", "build mode to use (default, c-shared)")
1504+
buildMode := flag.String("buildmode", "", "build mode to use (default, c-shared, wasi-legacy)")
15051505
var stackSize uint64
15061506
flag.Func("stack-size", "goroutine stack size (if unknown at compile time)", func(s string) error {
15071507
size, err := bytesize.Parse(s)

Diff for: main_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,15 @@ func TestWasmExport(t *testing.T) {
594594
noOutput: true, // wasm-unknown cannot produce output
595595
command: true,
596596
},
597+
// Test buildmode=wasi-legacy with WASI.
598+
{
599+
name: "WASIp1-legacy",
600+
target: "wasip1",
601+
buildMode: "wasi-legacy",
602+
scheduler: "none",
603+
file: "wasmexport-noscheduler.go",
604+
command: true,
605+
},
597606
}
598607

599608
for _, tc := range tests {

Diff for: src/runtime/runtime_wasmentry.go

+9
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ func wasmEntryReactor() {
5252
}
5353
}
5454

55+
// This is the _start entry point, when using -buildmode=wasi-legacy.
56+
func wasmEntryLegacy() {
57+
// These need to be initialized early so that the heap can be initialized.
58+
initializeCalled = true
59+
heapStart = uintptr(unsafe.Pointer(&heapStartSymbol))
60+
heapEnd = uintptr(wasm_memory_size(0) * wasmPageSize)
61+
run()
62+
}
63+
5564
// Whether the runtime was initialized by a call to _initialize or _start.
5665
var initializeCalled bool
5766

0 commit comments

Comments
 (0)