Skip to content

Commit

Permalink
wasi wip
Browse files Browse the repository at this point in the history
Change-Id: Ia1c96a8461c7c11b8fcb173f9108a5ffbc277510
  • Loading branch information
neelance committed Mar 19, 2020
1 parent 691118e commit c47c426
Show file tree
Hide file tree
Showing 92 changed files with 2,308 additions and 118 deletions.
14 changes: 14 additions & 0 deletions misc/wasm/go_wasi_wasm_exec
@@ -0,0 +1,14 @@
#!/bin/bash
# Copyright 2018 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.

SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

exec wasmtime --dir=/ --env=PWD=$PWD $1 -- "${@:2}"
4 changes: 2 additions & 2 deletions src/cmd/internal/obj/wasm/wasmobj.go
Expand Up @@ -828,7 +828,7 @@ func regAddr(reg int16) obj.Addr {
// Most of the Go functions has a single parameter (PC_B) in
// Wasm ABI. This is a list of exceptions.
var notUsePC_B = map[string]bool{
"_rt0_wasm_js": true,
"_rt0_wasm_wasi": true,
"wasm_export_run": true,
"wasm_export_resume": true,
"wasm_export_getsp": true,
Expand Down Expand Up @@ -877,7 +877,7 @@ func assemble(ctxt *obj.Link, s *obj.LSym, newprog obj.ProgAlloc) {
// Function starts with declaration of locals: numbers and types.
// Some functions use a special calling convention.
switch s.Name {
case "_rt0_wasm_js", "wasm_export_run", "wasm_export_resume", "wasm_export_getsp", "wasm_pc_f_loop",
case "_rt0_wasm_wasi", "wasm_export_run", "wasm_export_resume", "wasm_export_getsp", "wasm_pc_f_loop",
"runtime.wasmMove", "runtime.wasmZero", "runtime.wasmDiv", "runtime.wasmTruncS", "runtime.wasmTruncU", "memeqbody":
varDecls = []*varDecl{}
useAssemblyRegMap()
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/link/internal/ld/dwarf.go
Expand Up @@ -1583,7 +1583,7 @@ func dwarfEnabled(ctxt *Link) bool {
if *FlagS && ctxt.HeadType != objabi.Hdarwin {
return false
}
if ctxt.HeadType == objabi.Hplan9 || ctxt.HeadType == objabi.Hjs {
if ctxt.HeadType == objabi.Hplan9 || ctxt.HeadType == objabi.Hjs || ctxt.HeadType == objabi.Hwasi {
return false
}

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/link/internal/ld/sym.go
Expand Up @@ -67,7 +67,7 @@ func (ctxt *Link) computeTLSOffset() {
default:
log.Fatalf("unknown thread-local storage offset for %v", ctxt.HeadType)

case objabi.Hplan9, objabi.Hwindows, objabi.Hjs, objabi.Haix:
case objabi.Hplan9, objabi.Hwindows, objabi.Hjs, objabi.Hwasi, objabi.Haix:
break

case objabi.Hlinux,
Expand Down
21 changes: 10 additions & 11 deletions src/cmd/link/internal/wasm/asm.go
Expand Up @@ -55,7 +55,8 @@ type wasmFuncType struct {
}

var wasmFuncTypes = map[string]*wasmFuncType{
"_rt0_wasm_js": {Params: []byte{}}, //
"_rt0_wasm_wasi": {Params: []byte{}}, //
"wasm_export__start": {}, //
"wasm_export_run": {Params: []byte{I32, I32}}, // argc, argv
"wasm_export_resume": {Params: []byte{}}, //
"wasm_export_getsp": {Results: []byte{I32}}, // sp
Expand Down Expand Up @@ -348,18 +349,16 @@ func writeGlobalSec(ctxt *ld.Link) {
func writeExportSec(ctxt *ld.Link, lenHostImports int) {
sizeOffset := writeSecHeader(ctxt, sectionExport)

writeUleb128(ctxt.Out, 4) // number of exports
writeUleb128(ctxt.Out, 2) // number of exports

for _, name := range []string{"run", "resume", "getsp"} {
idx := uint32(lenHostImports) + uint32(ctxt.Syms.ROLookup("wasm_export_"+name, 0).Value>>16) - funcValueOffset
writeName(ctxt.Out, name) // inst.exports.run/resume/getsp in wasm_exec.js
ctxt.Out.WriteByte(0x00) // func export
writeUleb128(ctxt.Out, uint64(idx)) // funcidx
}
idx := uint32(lenHostImports) + uint32(ctxt.Syms.ROLookup("_rt0_wasm_wasi", 0).Value>>16) - funcValueOffset
writeName(ctxt.Out, "_start") // inst.exports.run/resume/getsp in wasm_exec.js
ctxt.Out.WriteByte(0x00) // func export
writeUleb128(ctxt.Out, uint64(idx)) // funcidx

writeName(ctxt.Out, "mem") // inst.exports.mem in wasm_exec.js
ctxt.Out.WriteByte(0x02) // mem export
writeUleb128(ctxt.Out, 0) // memidx
writeName(ctxt.Out, "memory") // inst.exports.mem in wasm_exec.js
ctxt.Out.WriteByte(0x02) // mem export
writeUleb128(ctxt.Out, 0) // memidx

writeSecSize(ctxt, sizeOffset)
}
Expand Down
21 changes: 21 additions & 0 deletions src/crypto/rand/rand_wasi.go
@@ -0,0 +1,21 @@
// Copyright 2018 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 rand

import "syscall"

func init() {
Reader = &reader{}
}

type reader struct{}

func (r *reader) Read(b []byte) (int, error) {
err := syscall.RandomGet(b)
if err != nil {
return 0, err
}
return len(b), nil
}
2 changes: 1 addition & 1 deletion src/crypto/x509/root_unix.go
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build aix dragonfly freebsd js,wasm linux netbsd openbsd solaris
// +build aix dragonfly freebsd js,wasm linux netbsd openbsd solaris wasi

package x509

Expand Down
8 changes: 8 additions & 0 deletions src/crypto/x509/root_wasi.go
@@ -0,0 +1,8 @@
// Copyright 2018 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 x509

// Possible certificate files; stop after finding one.
var certFiles = []string{}
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build js,wasm
// +build js,wasm wasi

package poll

Expand Down
2 changes: 1 addition & 1 deletion src/internal/poll/fd_fsync_posix.go
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build aix dragonfly freebsd js,wasm linux netbsd openbsd solaris
// +build aix dragonfly freebsd js,wasm linux netbsd openbsd solaris wasi

package poll

Expand Down
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build js,wasm
// +build js,wasm wasi

package poll

Expand Down
2 changes: 1 addition & 1 deletion src/internal/poll/fd_posix.go
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris windows
// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris wasi windows

package poll

Expand Down
2 changes: 1 addition & 1 deletion src/internal/poll/fd_unix.go
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris
// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris wasi

package poll

Expand Down
30 changes: 30 additions & 0 deletions src/internal/poll/fd_wasi.go
@@ -0,0 +1,30 @@
// Copyright 2019 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 poll

import "syscall"

// ReadDir wraps syscall.ReadDir.
// We treat this like an ordinary system call rather than a call
// that tries to fill the buffer.
func (fd *FD) ReadDir(buf []byte, cookie syscall.Dircookie_t) (int, error) {
if err := fd.incref(); err != nil {
return 0, err
}
defer fd.decref()
for {
n, err := syscall.ReadDir(fd.Sysfd, buf, cookie)
if err != nil {
n = 0
if err == syscall.EAGAIN && fd.pd.pollable() {
if err = fd.pd.waitRead(fd.isFile); err == nil {
continue
}
}
}
// Do not call eofError; caller does not expect to see io.EOF.
return n, err
}
}
2 changes: 1 addition & 1 deletion src/internal/poll/hook_unix.go
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris
// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris wasi

package poll

Expand Down
2 changes: 1 addition & 1 deletion src/internal/poll/sys_cloexec.go
Expand Up @@ -5,7 +5,7 @@
// This file implements sysSocket and accept for platforms that do not
// provide a fast path for setting SetNonblock and CloseOnExec.

// +build aix darwin js,wasm solaris
// +build aix darwin js,wasm solaris wasi

package poll

Expand Down
9 changes: 9 additions & 0 deletions src/internal/syscall/unix/nonblocking_wasi.go
@@ -0,0 +1,9 @@
// Copyright 2018 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 unix

func IsNonblock(fd int) (nonblocking bool, err error) {
return false, nil
}
2 changes: 1 addition & 1 deletion src/mime/type_unix.go
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris
// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris wasi

package mime

Expand Down
2 changes: 1 addition & 1 deletion src/net/error_posix.go
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris windows
// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris wasi windows

package net

Expand Down
2 changes: 1 addition & 1 deletion src/net/error_unix.go
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build aix darwin dragonfly freebsd js linux netbsd openbsd solaris
// +build aix darwin dragonfly freebsd js linux netbsd openbsd solaris wasi

package net

Expand Down
2 changes: 1 addition & 1 deletion src/net/file_stub.go
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build js,wasm
// +build js,wasm wasi

package net

Expand Down
2 changes: 1 addition & 1 deletion src/net/hook_unix.go
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris
// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris wasi

package net

Expand Down
2 changes: 0 additions & 2 deletions src/net/http/roundtrip.go
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build !js !wasm

package http

// RoundTrip implements the RoundTripper interface.
Expand Down
2 changes: 1 addition & 1 deletion src/net/interface_stub.go
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build js,wasm
// +build js,wasm wasi

package net

Expand Down
2 changes: 1 addition & 1 deletion src/net/internal/socktest/switch_unix.go
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris
// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris wasi

package socktest

Expand Down
2 changes: 1 addition & 1 deletion src/net/internal/socktest/sys_unix.go
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris
// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris wasi

package socktest

Expand Down
2 changes: 1 addition & 1 deletion src/net/iprawsock_posix.go
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris windows
// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris wasi windows

package net

Expand Down
2 changes: 1 addition & 1 deletion src/net/ipsock_posix.go
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris windows
// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris wasi windows

package net

Expand Down
2 changes: 1 addition & 1 deletion src/net/lookup_fake.go
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build js,wasm
// +build js,wasm wasi

package net

Expand Down
2 changes: 1 addition & 1 deletion src/net/main_noconf_test.go
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build js,wasm plan9 windows
// +build js,wasm plan9 wasi windows

package net

Expand Down
2 changes: 1 addition & 1 deletion src/net/net_fake.go
Expand Up @@ -4,7 +4,7 @@

// Fake networking for js/wasm. It is intended to allow tests of other package to pass.

// +build js,wasm
// +build js,wasm wasi

package net

Expand Down
2 changes: 1 addition & 1 deletion src/net/port_unix.go
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris
// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris wasi

// Read system port mappings from /etc/services

Expand Down
2 changes: 1 addition & 1 deletion src/net/rawconn_stub_test.go
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build js,wasm plan9
// +build js,wasm plan9 wasi

package net

Expand Down
2 changes: 1 addition & 1 deletion src/net/sendfile_stub.go
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build aix darwin js,wasm netbsd openbsd
// +build aix darwin js,wasm netbsd openbsd wasi

package net

Expand Down
2 changes: 1 addition & 1 deletion src/net/sock_stub.go
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build aix js,wasm solaris
// +build aix js,wasm solaris wasi

package net

Expand Down

0 comments on commit c47c426

Please sign in to comment.