wasi

package
v0.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 28, 2026 License: BSD-3-Clause Imports: 19 Imported by: 0

Documentation

Overview

Package wasi is a WASI preview 1 (wasi_snapshot_preview1) host written against a byte-addressed guest memory and the engine's rootfs.FS.

It implements every function Go's wasip1 port imports (runtime, syscall, internal/syscall/unix), with the layouts and errno values of the snapshot-01 specification (docs/REFERENCES.md). It knows nothing of a WebAssembly runtime: a host calls Proc.Call with the raw arguments and a Memory, so the same code serves the native executor (as a wazero host module) and the browser executor (from a worker, over shared memory).

Index

Constants

View Source
const (
	F32 = 'f'
	F64 = 'F'
)

Value types beyond I32 and I64.

View Source
const (
	EADDRINUSE    = 3
	EADDRNOTAVAIL = 4
	EAFNOSUPPORT  = 5
	EALREADY      = 7
	ECONNABORTED  = 13
	ECONNREFUSED  = 14
	ECONNRESET    = 15
	EHOSTUNREACH  = 23
	EINPROGRESS   = 26
	EINTR         = 27
	EISCONN       = 30
	EMFILE        = 33
	EMSGSIZE      = 35
	ENETDOWN      = 38
	ENETUNREACH   = 40
	ENOBUFS       = 42
	ENOPROTOOPT   = 50
	ENOTCONN      = 53
	ENOTSOCK      = 57
	EPIPE         = 64
	EPROTOTYPE    = 67
	ETIMEDOUT     = 73

	EAI_NONAME  = 84
	EAI_SERVICE = 85
)

More errno values of wasi_snapshot_preview1 (enum order), and the getaddrinfo ones WasmEdge adds.

View Source
const (
	ESUCCESS     = 0
	E2BIG        = 1
	EACCES       = 2
	EAGAIN       = 6
	EBADF        = 8
	EEXIST       = 20
	EFAULT       = 21
	EINVAL       = 28
	EIO          = 29
	EISDIR       = 31
	ELOOP        = 32
	ENAMETOOLONG = 37
	ENOENT       = 44
	ENOSPC       = 51
	ENOSYS       = 52
	ENOTDIR      = 54
	ENOTEMPTY    = 55
	ENOTSUP      = 58
	EPERM        = 63
	EROFS        = 69
	ESPIPE       = 70
	EXDEV        = 75
	ENOTCAPABLE  = 76
)

Errno values of wasi_snapshot_preview1 (enum order of the spec).

View Source
const (
	I32 = 'i'
	I64 = 'I'
)

Param types of a function's core-wasm signature.

View Source
const MaxFDs = 1024

MaxFDs bounds the descriptors a process may hold.

View Source
const MaxParams = 17

MaxParams bounds a host function's core parameters: the canonical ABI flattens at most 16 (MAX_FLAT_PARAMS), plus one return-area pointer.

View Source
const PreviewModule = "wasi_snapshot_preview1"

PreviewModule is the import module of Funcs.

Variables

View Source
var FuncIndex = func() map[string]int {
	m := make(map[string]int, len(Funcs))
	for i, f := range Funcs {
		m[f.Name] = i
	}
	return m
}()

FuncIndex maps a name to its index in Funcs.

View Source
var Funcs = []Func{
	{"args_get", "ii", false},
	{"args_sizes_get", "ii", false},
	{"environ_get", "ii", false},
	{"environ_sizes_get", "ii", false},
	{"clock_res_get", "ii", false},
	{"clock_time_get", "iIi", false},
	{"fd_advise", "iIIi", false},
	{"fd_allocate", "iII", false},
	{"fd_close", "i", false},
	{"fd_datasync", "i", false},
	{"fd_fdstat_get", "ii", false},
	{"fd_fdstat_set_flags", "ii", false},
	{"fd_fdstat_set_rights", "iII", false},
	{"fd_filestat_get", "ii", false},
	{"fd_filestat_set_size", "iI", false},
	{"fd_filestat_set_times", "iIIi", false},
	{"fd_pread", "iiiIi", false},
	{"fd_prestat_get", "ii", false},
	{"fd_prestat_dir_name", "iii", false},
	{"fd_pwrite", "iiiIi", false},
	{"fd_read", "iiii", false},
	{"fd_readdir", "iiiIi", false},
	{"fd_renumber", "ii", false},
	{"fd_seek", "iIii", false},
	{"fd_sync", "i", false},
	{"fd_tell", "ii", false},
	{"fd_write", "iiii", false},
	{"path_create_directory", "iii", false},
	{"path_filestat_get", "iiiii", false},
	{"path_filestat_set_times", "iiiiIIi", false},
	{"path_link", "iiiiiii", false},
	{"path_open", "iiiiiIIii", false},
	{"path_readlink", "iiiiii", false},
	{"path_remove_directory", "iii", false},
	{"path_rename", "iiiiii", false},
	{"path_symlink", "iiiii", false},
	{"path_unlink_file", "iii", false},
	{"poll_oneoff", "iiii", false},
	{"proc_exit", "i", true},
	{"proc_raise", "i", false},
	{"sched_yield", "", false},
	{"random_get", "ii", false},
	{"sock_accept", "iii", false},
	{"sock_recv", "iiiiii", false},
	{"sock_send", "iiiii", false},
	{"sock_shutdown", "ii", false},

	{"sock_open", "iii", false},
	{"sock_bind", "iii", false},
	{"sock_listen", "ii", false},
	{"sock_connect", "iii", false},
	{"sock_recv_from", "iiiiiiii", false},
	{"sock_send_to", "iiiiiii", false},
	{"sock_getsockopt", "iiiii", false},
	{"sock_setsockopt", "iiiii", false},
	{"sock_getlocaladdr", "iii", false},
	{"sock_getpeeraddr", "iii", false},
	{"sock_getaddrinfo", "iiiiiiii", false},
}

Funcs lists the imports, in a fixed order a host may index by.

Functions

func CheckBlock added in v0.13.0

func CheckBlock(ptr, align, size uint32, memSize uint64) error

CheckBlock refuses a block a guest's allocator returned unless it is aligned and lies inside a memory of memSize bytes. The guest chooses ptr, so the sum is computed without overflow.

Types

type Config

type Config struct {
	Args, Env []string
	FS        *rootfs.FS
	// Mounts are further trees, each preopened under its own path after
	// "/" (fd 4 onwards), as wasmtime's --dir host::guest does: a guest
	// resolves an absolute path against the longest matching preopen.
	Mounts         []Mount
	Stdin          io.Reader
	Stdout, Stderr io.Writer
	// Now is the realtime clock (default time.Now).
	Now func() time.Time
	// Rand is the randomness source (default crypto/rand).
	Rand io.Reader
	// Net is the container's place on a network; nil means no network
	// (sock_open fails with ENOTSUP).
	Net netdriver.Endpoint
	// Modules are the host modules served beside wasi_snapshot_preview1,
	// in the order of the import table (Imports); an executor passes the
	// same list it defined the imports from.
	Modules []HostModule
	// Caps are the capabilities the process was started with ("gpu").
	Caps []string
	// Parent is the process that started this one, if any.
	Parent *Proc
}

Config is a process's world.

type Func

type Func struct {
	Name   string
	Params string // one of I32/I64 per parameter
	// NoResult is set for proc_exit; every other function returns an errno.
	NoResult bool
}

Func describes one import.

type HostFunc added in v0.13.0

type HostFunc struct {
	Name string
	// Params has one of I32, I64, F32, F64 per parameter.
	Params string
	// Result is "" (no result) or one of I32, I64, F32, F64. The value is
	// Result.Ret; an F32 or F64 travels as its IEEE-754 bits.
	Result string
}

HostFunc is one function of a host module, with its core-wasm signature.

type HostModule added in v0.13.0

type HostModule interface {
	// Module is the import module name, e.g. "wasi_ephemeral_nn".
	Module() string
	// Funcs lists the functions, in an order the module indexes by.
	Funcs() []HostFunc
	// Open makes the module's state for process p, at p's first call into
	// it. It never fails: a process denied the module (no capability) gets
	// an Instance that answers with the module's own error values.
	Open(p *Proc) Instance
}

HostModule is an import module a guest may link against.

type Import added in v0.13.0

type Import struct {
	Module, Name   string
	Params, Result string
}

Import is one entry of a process's import table.

func Imports added in v0.13.0

func Imports(mods []HostModule) ([]Import, error)

Imports returns the import table an executor defines and indexes by: Funcs first (wasi_snapshot_preview1, result I32 except proc_exit), then each module's functions in order. It refuses a malformed module: an empty or duplicate module name, PreviewModule itself, a duplicate function name, a bad type or more than MaxParams parameters.

type Instance added in v0.13.0

type Instance interface {
	// Call runs function fn of Funcs with one value per parameter (I32
	// in the low 32 bits, F32/F64 as bits).
	Call(fn int, args []uint64, m Memory) Result
	// Close releases the state when the process ends. It may run while a
	// Wait the instance returned is still pending; a later Resume must
	// then fail rather than touch released state.
	Close()
}

Instance is a host module's state in one process.

type Memory

type Memory interface {
	Read(off, n uint32) ([]byte, bool)
	Write(off uint32, b []byte) bool
}

Memory is the guest's linear memory. Read returns a slice valid until the next call on the Memory; both return false out of bounds.

type Mount added in v0.8.0

type Mount struct {
	Path     string
	FS       *rootfs.FS
	ReadOnly bool
}

Mount is a tree preopened at Path; ReadOnly refuses every change made through it with EROFS.

type Proc

type Proc struct {
	// contains filtered or unexported fields
}

Proc is one guest process's WASI state.

func New

func New(cfg Config) *Proc

New returns a process with stdin, stdout, stderr and "/" preopened as 3.

func (*Proc) Call

func (p *Proc) Call(fn int, args []uint64, m Memory) Result

Call runs import number fn of Funcs with args (one per parameter, i32 values in the low 32 bits).

func (*Proc) CallImport added in v0.13.0

func (p *Proc) CallImport(i int, args []uint64, m Memory) Result

CallImport runs entry i of the table Imports(cfg.Modules) returned. Entries past Funcs go to their module's Instance, opened on first use.

func (*Proc) Close added in v0.10.0

func (p *Proc) Close()

Close releases what the process holds beyond its memory: its host modules' instances, its sockets and their goroutines. An executor calls it when the process ends.

func (*Proc) HasCap added in v0.13.0

func (p *Proc) HasCap(c string) bool

HasCap reports whether the process was started with capability c (e.g. "gpu", from run --gpu).

func (*Proc) Instance added in v0.13.0

func (p *Proc) Instance(module string) Instance

Instance returns the opened instance of the module named module in this process, or nil; a child's module may look up its parent's this way.

func (*Proc) Parent added in v0.13.0

func (p *Proc) Parent() *Proc

Parent is the process that started this one (nil for a container's first process), so a module can share state across a process tree.

type Realloc added in v0.13.0

type Realloc struct {
	Align, Size uint32
	Then        func(ptr uint32, m Memory) Result
}

Realloc asks the executor to allocate Size bytes aligned to Align in the guest, with the guest's cabi_realloc export (the canonical ABI's allocator), and to return Then(ptr) instead. The executor checks the block with CheckBlock first; a guest without the export, or a block outside its memory, traps the guest.

type Result

type Result struct {
	Errno uint32
	// Sleep asks the host to block the guest this long before returning
	// (poll_oneoff on clocks). The host may cut it short on a kill.
	Sleep time.Duration
	// Wait, when set, asks the host to block until Wait is closed, Sleep
	// (if not zero) has passed, or the process is killed, and then to
	// return what Resume returns instead (which may ask to wait again).
	// The call itself never blocks: sockets wait this way.
	Wait   <-chan struct{}
	Resume func(m Memory) Result
	// Exit is set by proc_exit, with Code.
	Exit bool
	Code uint32
	// Ret is a host-module function's result (HostFunc.Result); the
	// wasi_snapshot_preview1 functions return Errno instead.
	Ret uint64
	// Realloc, when set, asks the host to allocate in the guest first
	// (see Realloc); Wait and Realloc are never both set.
	Realloc *Realloc
	// Trap, when not empty, ends the guest with a trap of that message (a
	// host module's answer to a call the canonical ABI says must trap).
	Trap string
}

Result is what a call returned.

Directories

Path Synopsis
Package hostmodtest is a host module for tests ("wctr_test"): the executors' tests run testdata/wasm/hostmod against it, natively and in the browser, so both sides of the seam are exercised by one guest.
Package hostmodtest is a host module for tests ("wctr_test"): the executors' tests run testdata/wasm/hostmod against it, natively and in the browser, so both sides of the seam are exercised by one guest.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL