aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/internal
diff options
context:
space:
mode:
authorChris O'Hara <cohara87@gmail.com>2023-05-08 17:07:18 +1000
committerGopher Robot <gobot@golang.org>2023-05-25 00:12:40 +0000
commitc5c2184538411c8cf7abc4e536fbe7af8b0307f5 (patch)
tree8cf138711de09b7d23c1a489482cdd7773af5858 /src/runtime/internal
parent04c628935d1487632f903e4e0688fb7a34063752 (diff)
downloadgo-c5c2184538411c8cf7abc4e536fbe7af8b0307f5.tar.xz
runtime: implement wasip1 netpoll
Implements netpoll using WASI's poll_oneoff system call. This enables non-blocking I/O support for wasip1. Change-Id: Ie395fa49d651c8b8262d485e2847dd65b0a10bc6 Reviewed-on: https://go-review.googlesource.com/c/go/+/493357 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com> Reviewed-by: Julien Fabre <ju.pryz@gmail.com> Auto-Submit: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com> Run-TryBot: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/runtime/internal')
-rw-r--r--src/runtime/internal/wasitest/host_test.go14
-rw-r--r--src/runtime/internal/wasitest/nonblock_test.go96
-rw-r--r--src/runtime/internal/wasitest/testdata/nonblock.go48
3 files changed, 158 insertions, 0 deletions
diff --git a/src/runtime/internal/wasitest/host_test.go b/src/runtime/internal/wasitest/host_test.go
new file mode 100644
index 0000000000..ca4ef8f4e8
--- /dev/null
+++ b/src/runtime/internal/wasitest/host_test.go
@@ -0,0 +1,14 @@
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package wasi_test
+
+import "flag"
+
+var target string
+
+func init() {
+ // The dist test runner passes -target when running this as a host test.
+ flag.StringVar(&target, "target", "", "")
+}
diff --git a/src/runtime/internal/wasitest/nonblock_test.go b/src/runtime/internal/wasitest/nonblock_test.go
new file mode 100644
index 0000000000..887baab33f
--- /dev/null
+++ b/src/runtime/internal/wasitest/nonblock_test.go
@@ -0,0 +1,96 @@
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package wasi_test
+
+import (
+ "bufio"
+ "fmt"
+ "io"
+ "math/rand"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "syscall"
+ "testing"
+)
+
+// This test creates a set of FIFOs and writes to them in reverse order. It
+// checks that the output order matches the write order. The test binary opens
+// the FIFOs in their original order and spawns a goroutine for each that reads
+// from the FIFO and writes the result to stderr. If I/O was blocking, all
+// goroutines would be blocked waiting for one read call to return, and the
+// output order wouldn't match.
+
+type fifo struct {
+ file *os.File
+ path string
+}
+
+func TestNonblock(t *testing.T) {
+ if target != "wasip1/wasm" {
+ t.Skip()
+ }
+
+ switch os.Getenv("GOWASIRUNTIME") {
+ case "wazero", "":
+ t.Skip("wazero does not support non-blocking I/O")
+ case "wasmer":
+ t.Skip("wasmer does not support non-blocking I/O")
+ }
+
+ args := []string{"run", "./testdata/nonblock.go"}
+
+ fifos := make([]*fifo, 8)
+ for i := range fifos {
+ path := filepath.Join(t.TempDir(), fmt.Sprintf("wasip1-nonblock-fifo-%d-%d", rand.Uint32(), i))
+ if err := syscall.Mkfifo(path, 0666); err != nil {
+ t.Fatal(err)
+ }
+
+ file, err := os.OpenFile(path, os.O_RDWR, 0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer file.Close()
+
+ args = append(args, path)
+ fifos[len(fifos)-i-1] = &fifo{file, path}
+ }
+
+ subProcess := exec.Command("go", args...)
+
+ subProcess.Env = append(os.Environ(), "GOOS=wasip1", "GOARCH=wasm")
+
+ pr, pw := io.Pipe()
+ defer pw.Close()
+
+ subProcess.Stderr = pw
+
+ if err := subProcess.Start(); err != nil {
+ t.Fatal(err)
+ }
+
+ scanner := bufio.NewScanner(pr)
+ if !scanner.Scan() {
+ t.Fatal("expected line:", scanner.Err())
+ } else if scanner.Text() != "waiting" {
+ t.Fatal("unexpected output:", scanner.Text())
+ }
+
+ for _, fifo := range fifos {
+ if _, err := fifo.file.WriteString(fifo.path + "\n"); err != nil {
+ t.Fatal(err)
+ }
+ if !scanner.Scan() {
+ t.Fatal("expected line:", scanner.Err())
+ } else if scanner.Text() != fifo.path {
+ t.Fatal("unexpected line:", scanner.Text())
+ }
+ }
+
+ if err := subProcess.Wait(); err != nil {
+ t.Fatal(err)
+ }
+}
diff --git a/src/runtime/internal/wasitest/testdata/nonblock.go b/src/runtime/internal/wasitest/testdata/nonblock.go
new file mode 100644
index 0000000000..947abe7fcf
--- /dev/null
+++ b/src/runtime/internal/wasitest/testdata/nonblock.go
@@ -0,0 +1,48 @@
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+ "os"
+ "sync"
+)
+
+func main() {
+ ready := make(chan struct{})
+
+ var wg sync.WaitGroup
+ for _, path := range os.Args[1:] {
+ f, err := os.Open(path)
+ if err != nil {
+ panic(err)
+ }
+
+ spawnWait := make(chan struct{})
+
+ wg.Add(1)
+ go func(f *os.File) {
+ defer f.Close()
+ defer wg.Done()
+
+ close(spawnWait)
+
+ <-ready
+
+ var buf [256]byte
+ n, err := f.Read(buf[:])
+ if err != nil {
+ panic(err)
+ }
+ os.Stderr.Write(buf[:n])
+ }(f)
+
+ // Spawn one goroutine at a time.
+ <-spawnWait
+ }
+
+ println("waiting")
+ close(ready)
+ wg.Wait()
+}