Skip to content
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

internal: some WIP on being able to compile wazero for MCUs using TinyGo #1855

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion examples/basic/add.go
Original file line number Diff line number Diff line change
@@ -7,7 +7,10 @@ import (
"flag"
"fmt"
"log"

//"runtime"
"strconv"
"time"

"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
@@ -26,20 +29,25 @@ var addWasm []byte
// Since addWasm was compiled with TinyGo's `wasi` target, we need to configure
// WASI host imports.
func main() {
time.Sleep(5 * time.Second)
log.Println("Starting wazero on tinygo...")

// Parse positional arguments.
flag.Parse()

// Choose the context to use for function calls.
ctx := context.Background()

// Create a new WebAssembly Runtime.
r := wazero.NewRuntime(ctx)
r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfigInterpreter())
defer r.Close(ctx) // This closes everything this Runtime created.

// Instantiate WASI, which implements host functions needed for TinyGo to
// implement `panic`.
wasi_snapshot_preview1.MustInstantiate(ctx, r)

//runtime.GC()

// Instantiate the guest Wasm into the same runtime. It exports the `add`
// function, implemented in WebAssembly.
mod, err := r.Instantiate(ctx, addWasm)
Binary file modified examples/basic/testdata/add.wasm
Binary file not shown.
2 changes: 2 additions & 0 deletions internal/filecache/file_cache.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !tinygo

package filecache

import (
36 changes: 36 additions & 0 deletions internal/filecache/file_cache_tinygo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//go:build tinygo

package filecache

import (
"errors"

"io"
)

var errNotYetSupported = errors.New("not yet supported")

// New returns a new Cache implemented by an in-memory cache. Possibly Flash memory...
func New(dir string) Cache {
return newMemoryCache()
}

func newMemoryCache() *memoryCache {
return &memoryCache{}
}

// memoryCache persists compiled functions into memory.
type memoryCache struct {
}

func (mc *memoryCache) Get(key Key) (content io.ReadCloser, ok bool, err error) {
return nil, false, errNotYetSupported
}

func (mc *memoryCache) Add(key Key, content io.Reader) (err error) {
return errNotYetSupported
}

func (mc *memoryCache) Delete(key Key) (err error) {
return errNotYetSupported
}
2 changes: 2 additions & 0 deletions internal/platform/mmap_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !tinygo

package platform

import (
23 changes: 23 additions & 0 deletions internal/platform/mmap_tinygo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//go:build tinygo

package platform

import "errors"

var errNotYetSupported = errors.New("not yet supported")

func munmapCodeSegment(code []byte) error {
return errNotYetSupported
}

func mmapCodeSegmentAMD64(size int) ([]byte, error) {
return nil, errNotYetSupported
}

func mmapCodeSegmentARM64(size int) ([]byte, error) {
return nil, errNotYetSupported
}

func MprotectRX(b []byte) (err error) {
return errNotYetSupported
}
2 changes: 1 addition & 1 deletion internal/platform/mmap_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build darwin || linux || freebsd
//go:build (darwin || linux || freebsd) && !tinygo

package platform

2 changes: 1 addition & 1 deletion internal/platform/mmap_unsupported.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !(darwin || linux || freebsd || windows)
//go:build !(darwin || linux || freebsd || windows || tinygo)

package platform

11 changes: 11 additions & 0 deletions internal/platform/mremap_tinygo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build tinygo

package platform

func remapCodeSegmentAMD64(code []byte, size int) ([]byte, error) {
return nil, errNotYetSupported
}

func remapCodeSegmentARM64(code []byte, size int) ([]byte, error) {
return nil, errNotYetSupported
}
2 changes: 1 addition & 1 deletion internal/platform/mremap_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build darwin || linux || freebsd
//go:build (darwin || linux || freebsd) && !tinygo

package platform

2 changes: 1 addition & 1 deletion internal/sock/sock_supported.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !plan9 && !js
//go:build !plan9 && !js && !tinygo

package sock

2 changes: 1 addition & 1 deletion internal/sock/sock_unsupported.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build plan9 || js
//go:build plan9 || js || tinygo

package sock

2 changes: 1 addition & 1 deletion internal/sysfs/datasync_linux.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build linux
//go:build linux && !tinygo

package sysfs

2 changes: 1 addition & 1 deletion internal/sysfs/datasync_unsupported.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !linux
//go:build !linux || tinygo

package sysfs

2 changes: 2 additions & 0 deletions internal/sysfs/dirfs.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !tinygo

package sysfs

import (
11 changes: 11 additions & 0 deletions internal/sysfs/dirfs_tinygo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build tinygo

package sysfs

import (
experimentalsys "github.com/tetratelabs/wazero/experimental/sys"
)

func DirFS(dir string) experimentalsys.FS {
return nil
}
2 changes: 1 addition & 1 deletion internal/sysfs/file_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build unix
//go:build unix && !tinygo

package sysfs

2 changes: 1 addition & 1 deletion internal/sysfs/file_unsupported.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !unix && !windows
//go:build (!unix && !windows) || tinygo

package sysfs

2 changes: 1 addition & 1 deletion internal/sysfs/futimens.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build linux || darwin
//go:build (linux || darwin) && !tinygo

package sysfs

2 changes: 2 additions & 0 deletions internal/sysfs/futimens_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !tinygo

package sysfs

import (
2 changes: 1 addition & 1 deletion internal/sysfs/futimens_unsupported.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !windows && !linux && !darwin
//go:build (!windows && !linux && !darwin) || tinygo

package sysfs

2 changes: 1 addition & 1 deletion internal/sysfs/ino.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !windows && !plan9
//go:build !windows && !plan9 && !tinygo

package sysfs

15 changes: 15 additions & 0 deletions internal/sysfs/ino_tinygo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build tinygo

package sysfs

import (
"io/fs"

experimentalsys "github.com/tetratelabs/wazero/experimental/sys"
"github.com/tetratelabs/wazero/sys"
)

// inoFromFileInfo uses stat to get the inode information of the file.
func inoFromFileInfo(dirPath string, info fs.FileInfo) (ino sys.Inode, errno experimentalsys.Errno) {
return 0, 0
}
15 changes: 15 additions & 0 deletions internal/sysfs/nonblock_tinygo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build tinygo

package sysfs

import (
"github.com/tetratelabs/wazero/experimental/sys"
)

func setNonblock(fd uintptr, enable bool) sys.Errno {
return 0
}

func isNonblock(f *osFile) bool {
return false
}
2 changes: 1 addition & 1 deletion internal/sysfs/nonblock_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !windows && !plan9
//go:build !windows && !plan9 && !tinygo

package sysfs

2 changes: 2 additions & 0 deletions internal/sysfs/open_file_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !tinygo

package sysfs

import (
2 changes: 1 addition & 1 deletion internal/sysfs/open_file_unsupported.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !darwin && !linux && !windows && !illumos && !solaris && !freebsd
//go:build (!darwin && !linux && !windows && !illumos && !solaris && !freebsd) || tinygo

package sysfs

2 changes: 1 addition & 1 deletion internal/sysfs/poll.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build windows || linux || darwin
//go:build (windows || linux || darwin) && !tinygo

package sysfs

2 changes: 2 additions & 0 deletions internal/sysfs/poll_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !tinygo

package sysfs

import (
2 changes: 1 addition & 1 deletion internal/sysfs/poll_unsupported.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !linux && !darwin && !windows
//go:build (!linux && !darwin && !windows) || tinygo

package sysfs

2 changes: 1 addition & 1 deletion internal/sysfs/rename.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !windows && !plan9
//go:build !windows && !plan9 && !tinygo

package sysfs

11 changes: 11 additions & 0 deletions internal/sysfs/rename_tinygo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build tinygo

package sysfs

import (
"github.com/tetratelabs/wazero/experimental/sys"
)

func rename(from, to string) sys.Errno {
return 0
}
2 changes: 1 addition & 1 deletion internal/sysfs/sock_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build linux || darwin
//go:build (linux || darwin) && !tinygo

package sysfs

2 changes: 1 addition & 1 deletion internal/sysfs/sock_unsupported.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !linux && !darwin && !windows
//go:build (!linux && !darwin && !windows) || tinygo

package sysfs

2 changes: 1 addition & 1 deletion internal/sysfs/unlink.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !windows && !plan9
//go:build !windows && !plan9 && !tinygo

package sysfs

11 changes: 11 additions & 0 deletions internal/sysfs/unlink_tinygo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build tinygo

package sysfs

import (
"github.com/tetratelabs/wazero/experimental/sys"
)

func unlink(name string) (errno sys.Errno) {
return 0
}
2 changes: 1 addition & 1 deletion internal/wasm/memory.go
Original file line number Diff line number Diff line change
@@ -192,7 +192,7 @@ func (m *MemoryInstance) Grow(delta uint32) (result uint32, ok bool) {
return currentPages, true
} else { // We already have the capacity we need.
sp := (*reflect.SliceHeader)(unsafe.Pointer(&m.Buffer))
sp.Len = int(MemoryPagesToBytesNum(newPages))
sp.Len = lengthMemoryPages(newPages)
return currentPages, true
}
}
7 changes: 7 additions & 0 deletions internal/wasm/memorypages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build !tinygo

package wasm

func lengthMemoryPages(newPage uint32) int {
return int(MemoryPagesToBytesNum(newPage))
}
7 changes: 7 additions & 0 deletions internal/wasm/memorypages_tinygo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build tinygo

package wasm

func lengthMemoryPages(newPage uint32) uintptr {
return uintptr(MemoryPagesToBytesNum(newPage))
}