Categorygithub.com/stealthrocket/wasi-go
modulepackage
0.8.0
Repository: https://github.com/stealthrocket/wasi-go.git
Documentation: pkg.go.dev

# README

Build Go Reference Apache 2 License

WASI

The WebAssembly System Interface (WASI) is a set of standard system functions that allow WebAssembly modules to interact with the outside world (e.g. perform I/O, read clocks).

The WASI standard is under development. This repository provides a Go implementation of WASI preview 1 for Unix systems, and a command to run WebAssembly modules that use WASI.

Motivation

WASI preview 1 was sealed without a complete socket API, and WASI as a standard is still a moving target.

Some WebAssembly runtimes have taken the initiative to either extend WASI preview 1 or provide alternative solutions for capabilities that were missing from the core specification, enabling a wider range of applications to run as WebAssembly modules.

This package intends to bundle WASI preview 1 and these extensions with the wazero runtime, and more generally be a playground for experimentation with cutting-edge WASI features.

:electric_plug: Sockets

This library provides all the socket capabilities specified in WASI preview 1, as well as a full support for a socket API which is ABI-compatible with the extensions implemented in the wasmedge runtime.

Applications interested in taking advantage of the socket extensions may be interested in those libraries:

LanguageLibrary
Gostealthrocket/net
Pythonstealthrocket/timecraft (Python SDK)
Rustsecond-state/wasmedge_wasi_socket

:zap: Performance

The provided implementation of WASI is a thin zero-allocation layer around OS system calls. Non-blocking I/O is fully supported, allowing WebAssembly modules with an embedded scheduler (e.g. the Go runtime, or Rust Tokio scheduler) to schedule goroutines / green threads while waiting for I/O.

:battery: Experimentation

The library separates the implementation of WASI from the WebAssembly runtime host module, so that implementations of the provided WASI interface don't have to worry about ABI concerns. The design makes it easy to wrap and augment WASI, and keep up with the evolving WASI specification.

Non-Goals

wasi-go does not aim to be a drop-in replacement for the wasi_snapshot_preview1 package that ships with the wazero runtime. For example, the wasi-go package does not build on Windows, nor does it allow customization of the file systems via a fs.FS.

The following table describes how users should think about capabilities of wazero and wasi-go:

Featurewazero/imports/wasi_snapshot_preview1wasi-go/imports/wasi_snapshot_preview1
WASI preview 1
Windows Support
WasmEdge Socket Extensions

Usage

As a Command

A wasirun command is provided for running WebAssembly modules that use WASI host imports. It bundles the WASI implementation from this repository with the wazero runtime.

$ go install github.com/stealthrocket/wasi-go/cmd/wasirun@latest

The wasirun command has many options for controlling the capabilities of the WebAssembly module, and for tracing and profiling execution. See wasirun --help for details.

As a Library

The package layout is as follows:

To run a WebAssembly module, it's also necessary to prepare clocks and "preopens" (files/directories that the WebAssembly module can access). To see how it all fits together, see the implementation of the wasirun command.

With Go

As the providers of a Go implementation of WASI, we're naturally interested in Go's support for WebAssembly and WASI, and are championing the efforts to make Go a first class citizen in the ecosystem (along with Rust and Zig).

Go v1.21, scheduled for release in August 2023, has native support for WebAssembly and WASI. To test these features before release, use gotip in place of go:

package main

import "fmt"

func main() {
	fmt.Println("Hello, World!")
}
$ GOOS=wasip1 GOARCH=wasm gotip build -o hello.wasm hello.go
$ wasirun hello.wasm
Hello, World!

This repository bundles a script that can be used to skip the go build step.

Contributing

Pull requests are welcome! Anything that is not a simple fix would probably benefit from being discussed in an issue first.

Remember to be respectful and open minded!

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

FS constructs a fs.FS file system backed by a wasi system.
MakeErrno converts a Go error to a WASI errno value in a way that is portable across platforms.
No description provided by the author
MakeSubscriptionClock makes a Subscription for ClockEvent events.
MakeSubscriptionFDReadWrite makes a Subscription for FDReadEvent or FDWriteEvent events.
SizesGet is a helper function used to implement the ArgsSizesGet and EnvironSizesGet methods of the System interface.
Trace wraps a System to log all calls to its methods in a human-readable format to the given io.Writer.
WithTracerStringSize sets the number of bytes to print when printing strings.

# Constants

Abstime is a flag indicating that the timestamp provided in SubscriptionClock.Timeout is an absolute timestamp of clock SubscriptionClock.ID.
AccessTime means adjust the last data access timestamp to the value stored in FileStat.AccessTime.
AccessTimeNow means adjust the last data access timestamp to the time of clock Realtime.
No description provided by the author
AllRights is the set of all available rights.
No description provided by the author
Append indicates append mode; data written to the file is always appended to the file's end.
SOL_SOCKET level options.
BlockDeviceType is indicates that the file descriptor or file refers to a block device inode.
SOL_SOCKET level options.
No description provided by the author
CharacterDeviceType indicates that the file descriptor or file refers to a character device inode.
ClockEvent is an event type that indicates that the time value of clock SubscriptionClock.ID has reached timestamp SubscriptionClock.Timeout.
No description provided by the author
DirectoryRights are rights related to directories.
DirectoryType indicates that the file descriptor or file refers to a directory inode.
DontNeed indicates that the application expects that it will not access the specified data in the near future.
SOL_SOCKET level options.
DSync means write according to synchronized I/O data integrity completion.
E2BIG means an argument list is too long.
EACCES means permission is denied.
EADDRINUSE means an address is already in use.
EADDRNOTAVAIL means an address is not available.
EAFNOSUPPORT means an address family is not supported by a protocol family.
EAGAIN means the caller should try again.
EALREADY means a socket already connected.
EBADF means bad file number.
EBADMSG indicates that the caller is trying to read an unreadable message.
EBUSY means a device or resource busy.
ECANCELED means an operation was canceled.
ECHILD means no child processes.
ECONNABORTED means a connection was aborted.
ECONNREFUSED means connection was refused.
ECONNRESET means a connection was reset by peer.
EDEADLK indicates a deadlock condition.
EDESTADDRREQ means a destination address is required.
EDOM means a math argument is out of domain of func.
EDQUOT means a quota was exceeded.
EEXIST means a file exists.
EFAULT means bad address.
EFBIG indicates a file is too large.
EHOSTUNREACH means a host is unreachable.
EIDRM means identifier removed.
EILSEQ indicates an illegal byte sequence.
EINPROGRESS means a connection is already in progress.
EINTR means a system call was interrupted.
EINVAL means an argument was invalid.
EIO means an I/O error occurred.
EISCONN means a socket is already connected.
EISDIR means a file is a directory.
ELOOP indicates that there are too many symbolic links.
EMFILE indicates that there are too many open files.
EMLINK indicates that there are too many links.
EMSGSIZE means a message is too long.
EMULTIHOP means a multihop was attempted.
ENAMETOOLONG means a file name is too long.
ENETDOWN means a network interface is not configured.
ENETRESET means a network was dropped connection on reset.
ENETUNREACH means a network is unreachable.
ENFILE means a file table overflow occurred.
ENOBUFS means that no buffer space is available.
ENODEV means no such device.
ENOENT means no such file or directory.
ENOEXEC means an exec format error.
ENOLCK means that there are no record locks available.
ENOLINK means the link has been severed.
ENOMEM means out of memory.
ENOMSG means that there is no message of desired type.
ENOPROTOOPT means a protocol is not available.
ENOSPC means that there is no space left on a device.
ENOSYS means not implemented.
ENOTCAPABLE means capabilities are insufficient.
ENOTCONN means a socket is not connected.
ENOTDIR means a file is not a directory.
ENOTEMPTY means a directory is not empty.
ENOTRECOVERABLE means state is not recoverable.
ENOTSOCK means a socket operation was attempted on a non-socket.
ENOTSUP means not supported.
ENOTTY means not a typewriter.
ENXIO means no such device or address.
EOVERFLOW means the value is too large for defined data type.
EOWNERDEAD means an owner died.
EPERM means an operation is not permitted.
EPIPE means broken pipe.
EPROTO means a protocol error ocurred.
EPROTONOSUPPORT means a protocol is not supported.
EPROTOTYPE means that a protocol is the wrong type for socket.
ERANGE means a math result is not representable.
EROFS means a file system is read-only.
ESPIPE means a seek is illegal.
ESRCH means no such process.
ESTALE means a file handle is stale.
ESUCCESS indicates that no error occurred (system call completed successfully).
ETIMEDOUT means a connection timed out.
ETXTBSY means text file busy.
EXDEV means cross-device link.
FDAdviseRight is the right to invoke FDAdvise.
FDAllocateRight is the right to invoke FDAllocate.
FDDataSyncRight is the right to invoke FDDataSync.
FDFileStatGetRight is the right to invoke FDFileStatGet.
FDFileStatSetSizeRight is the right to invoke FDFileStatSetSize.
FDFileStatSetTimesRight is the right to invoke FDFileStatSetTimes.
FDReadDirRight is the right to invoke FDReadDir.
FDReadEvent is an event type that indicates that the file descriptor SubscriptionFDReadWrite.FD has data available for reading.
FDReadRight is the right to invoke FDRead and SockRecv.
FDSeekRight is the right to invoke FDSeek.
FDStatSetFlagsRight is the right to invoke FDStatSetFlags.
FDSyncRight is the right to invoke FDSync.
FDTellRight is the right to invoke FDTell, and the right to invoke FDSeek in such a way that the file offset remains unaltered (i.e.
FDWriteEvent is an event type that indicates that the file descriptor SubscriptionFDReadWrite.FD has data available for writing.
FDWriteRight is the right to invoke FDWrite and SockSend.
FileRights are rights related to files.
Hangup is a flag that indicates that the peer of this socket has closed or disconnected.
No description provided by the author
No description provided by the author
No description provided by the author
SOL_SOCKET level options.
SOL_SOCKET level options.
ModifyTime means adjust the last data modification timestamp to the value stored in FileStat.ModifyTime.
ModifyTimeNow means adjust the last data modification timestamp to the time of clock Realtime.
Monotonic is the store-wide monotonic clock, which is defined as a clock measuring real time, whose value cannot be adjusted and which cannot have negative clock jumps.
NonBlock indicates non-blocking mode.
NoReuse indicates that the application expects to access the specified data once and then not reuse it thereafter.
Normal indicates that the application has no advice to give on its behavior with respect to the specified data.
No description provided by the author
No description provided by the author
SOL_SOCKET level options.
OpenCreate means create a file if it does not exist.
OpenDirectory means fail if the path is not a directory.
OpenExclusive means fail if the file already exists.
OpenTruncate means truncate file to size 0.
No description provided by the author
PathCreateDirectoryRight is the right to invoke PathCreateDirectory.
PathCreateFileRight is (along with PathOpenRight) the right to invoke PathOpen with the OpenCreate flag.
PathFileStatGetRight is the right to invoke PathFileStatGet.
PathFileStatSetSizeRight is the right to change a file's size.
PathFileStatSetTimesRight is the right to invoke PathFileStatSetTimes.
PathLinkSourceRight is the right to invoke PathLink with the file descriptor as the source directory.
PathLinkTargetRight is the right to invoke PathLink with the file descriptor as the target directory.
PathOpenRight is the right to invoke PathOpen.
PathReadLinkRight is the right to invoke PathReadLink.
PathRemoveDirectoryRight is the right to invoke PathRemoveDirectory.
PathRenameSourceRight is the right to invoke PathRename with the file descriptor as the source directory.
PathRenameTargetRight is the right to invoke PathRename with the file descriptor as the target directory.
PathSymlinkRight is the right to invoke PathSymlink.
PathUnlinkFileRight is the right to invoke PathUnlinkFile.
PollFDReadWriteRight is the right to invoke PollOneOff.
PreOpenDir is a pre-opened directory.
ProcessCPUTimeID is the CPU-time clock associated with the current process.
SOL_SOCKET level options.
No description provided by the author
SOL_SOCKET level options.
SOL_SOCKET level options.
Random indicates that the application expects to access the specified data in a random order.
ReadRights are rights related to reads.
Realtime is the clock measuring real time.
SOL_SOCKET level options.
RecvDataTruncated indicates that message data has been truncated.
SOL_SOCKET level options.
RecvPeek indicates that SockRecv should return the message without removing it from the socket's receive queue.
SOL_SOCKET level options.
RecvWaitAll indicates that on byte-stream sockets, SockRecv should block until the full amount of data can be returned.
RegularFileType indicates that the file descriptor or file refers to a regular file inode.
SOL_SOCKET level options.
RSync indicates synchronized read I/O operations.
SeekCurrent seeks relative to current offset.
SeekEnd seeks relative to end of the file.
SeekStart seeks relative to the origin of the file.
SOL_SOCKET level options.
SOL_SOCKET level options.
Sequential indicates that the application expects to access the specified data sequentially from lower offsets to higher offsets.
ShutdownRD disables further receive operations.
ShutdownWR disables further send operations.
SIGABRT is the process abort signal.
SIGALRM indicates an alarm clock.
SIGBUS indicates access to an undefined portion of a memory object.
SIGCHLD indicates that a child process terminated, stopped, or continued.
SIGCONT indicates that execution should continue, if stopped.
SIGFPE indicates an erroneous arithmetic operation.
SIGHUP means hangup.
SIGILL means illegal instruction.
SIGINT is the terminate interrupt signal.
SIGKILL means kill.
SIGNONE means no signal.
SIGPIPE indicates a write on a pipe with no one to read it.
SIGPOLL means I/O is possible.
SIGPROF means profiling timer expired.
SIGPWR indicates power failure.
SIGQUIT is the terminal quit signal.
SIGSEGV indicates an invalid memory reference.
SIGSTOP means stop executing.
SIGSYS indicates a bad system call.
SIGTERM is the termination signal.
SIGTRAP is the trace/breakpoint trap.
SIGTSTP is the terminal stop signal.
SIGTTIN indicates that a background process is attempting read.
SIGTTOU indicates that a background process is attempting write.
SIGURG indicates that high bandwidth data is available at a socket.
SIGUSR1 is the user-defined signal 1.
SIGUSR2 is the user-defined signal 1.
SIGVTALRM means virtual timer expired.
SIGWINCH means window changed.
SIGXCPU means CPU time limit exceeded.
SIGXFSZ means file size limit exceeded.
SizeOfDirent is the size in bytes of directory entries when serialized to the output buffer of fd_readdir.
SockAccessRight is the right to invoke SockAccept.
SockConnectionRights are rights for connection sockets.
SocketDGramType indicates that the file descriptor or file refers to a datagram socket.
SOL_SOCKET.
SocketStreamType indicates that the file descriptor or file refers to a byte-stream socket.
SockListenRights are rights for listener sockets.
SockShutdownRight is the right to invoke SockShutdown.
No description provided by the author
SymbolicLinkType indicates that the file refers to a symbolic link inode.
SymlinkFollow means that as long as the resolved path corresponds to a symbolic link, it is expanded.
Sync means write according to synchronized I/O data integrity completion.
IPPROTO_TCP.
IPPROTO_TCP level options.
No description provided by the author
ThreadCPUTimeID is the CPU-time clock associated with the current thread.
TTYRights are rights related to terminals.
No description provided by the author
No description provided by the author
UnknownType indicates that the type of the file descriptor or file is unknown or is different from any of the other types specified.
No description provided by the author
No description provided by the author
WillNeed indicates that the application expects to access the specified data in the near future.
WriteRights are rights related to writes.

# Structs

AddressInfo is information about an address.
DirEntry is a directory entry.
Event is an event that occurred.
EventFDReadWrite is the contents of an event when event type is FDReadEvent or FDWriteEvent.
FDStat is file descriptor attributes.
FileStat are file attributes.
FileTable is a building block used to construct implementations of the System interface.
No description provided by the author
No description provided by the author
PreStat is information about a pre-opened capability.
PreStatDir is the contents of a PreStat when type is PreOpenDir.
SocketsNotSupported is a helper type intended to be embeded in implementations of the Sytem interface that do not support sockets.
Subscription is a subscription to an event.
SubscriptionClock is the contents of a subscription when event type is ClockEvent.
SubscriptionFDReadWrite is the contents of a subscription when event type is FDReadEvent or FDWriteEvent.
No description provided by the author

# Interfaces

Dir instances are returned by File.FDOpenDir and used to iterate over.
File is an interface used as constraint in the FileType generic type parameter.
SocketAddress is a socket address.
SocketOptionValue is a socket option value.
System is the WebAssembly System Interface (WASI).

# Type aliases

AddressInfoFlags are AddressInfo flags.
Advice is file or memory access pattern advisory information.
BytesValue is used to represent an arbitrary socket option value.
ClockID is an identifier for clocks.
Device is an identifier for a device containing a file system.
DirCookie is a reference to the offset of a directory entry.
DirNameLength is the type for the DirEntry.NameLength field.
Errno are the error codes returned by functions.
EventFDReadWriteFlags is the state of the file descriptor subscribed to with FDReadEvent or FDWriteEvent.
EventType is a type of a subscription to an event, or its occurrence.
ExitCode is the exit code generated by a process when exiting.
FD is a file descriptor handle.
FDFlags are file descriptor flags.
FileDelta is a relative offset within a file.
FileSize is a non-negative file size or length of a region within a file.
FileType is the type of a file descriptor or file.
FSTFlags indicate which file time attributes to adjust.
INode is a file serial number that is unique within its file system.
IntValue is an integer value.
IOVec is a slice of bytes.
LinkCount is the number of hard links to an INode.
LookupFlags determine the method of how paths are resolved.
OpenFlags are flags used by PathOpen.
Port is a port.
PreOpenType are identifiers for pre-opened capabilities.
Protocol is a socket protocol.
ProtocolFamily is a socket protocol family.
RIFlags are flags provided to SockRecv.
Rights are file descriptor rights, determining which actions may be performed.
ROFlags are flags returned by SockRecv.
SDFlags are flags provided to SockShutdown which indicate which channels on a socket to shut down.
SIFlags are flags provided to SockSend.
Signal is a signal condition.
Size represents a size.
SocketOption is a socket option that can be queried or set.
SocketOptionLevel controls the level that a socket option is applied at or queried from.
SocketType is a type of socket.
SubscriptionClockFlags are flags determining how to interpret the timestamp provided in SubscriptionClock.Timeout.
Timestamp is a timestamp in nanoseconds.
TimeValue is used to represent socket options with a duration value.
TracerOption configures a tracer.
UserData is a user-provided value that may be attached to objects that is retained when extracted from the implementation.
Whence is the position relative to which to set the offset of the file descriptor.