Skip to content

Commit

Permalink
wasi: introduce wasi.Errno type and switch error codes to constants (#72
Browse files Browse the repository at this point in the history
)
  • Loading branch information
achille-roussel authored Dec 12, 2021
1 parent 340e4d4 commit 5c23040
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 89 deletions.
249 changes: 171 additions & 78 deletions wasi/errno.go
Original file line number Diff line number Diff line change
@@ -1,82 +1,175 @@
package wasi

import "fmt"

// Errno is a type representing standard WASI error codes and implementing the
// error interface.
type Errno uint32

func (err Errno) Error() string {
if int(err) < len(errnoToString) {
return errnoToString[err]
}
return fmt.Sprintf("errno(%d)", uint32(err))
}

// WASI error codes
var (
ESUCCESS uint32 = 0
E2BIG uint32 = 1 /* Arg list too long */
EACCES uint32 = 2 /* Permission denied */
EADDRINUSE uint32 = 3 /* Address already in use */
EADDRNOTAVAIL uint32 = 4 /* Cannot assign requested address */
EAFNOSUPPORT uint32 = 5 /* Address family not supported by protocol */
EAGAIN uint32 = 6 /* Try again */
EALREADY uint32 = 7 /* Operation already in progress */
EBADF uint32 = 8 /* Bad file number */
EBADMSG uint32 = 9
EBUSY uint32 = 10
ECANCELED uint32 = 11
ECHILD uint32 = 12
ECONNABORTED uint32 = 13
ECONNREFUSED uint32 = 14
ECONNRESET uint32 = 15
EDEADLK uint32 = 16
EDESTADDRREQ uint32 = 17
EDOM uint32 = 18
EDQUOT uint32 = 19
EEXIST uint32 = 20
EFAULT uint32 = 21
EFBIG uint32 = 22
EHOSTUNREACH uint32 = 23
EIDRM uint32 = 24
EILSEQ uint32 = 25
EINPROGRESS uint32 = 26
EINTR uint32 = 27
EINVAL uint32 = 28 /* Invalid argument */
EIO uint32 = 29
EISCONN uint32 = 30
EISDIR uint32 = 31
ELOOP uint32 = 32
EMFILE uint32 = 33
EMLINK uint32 = 34
EMSGSIZE uint32 = 35
EMULTIHOP uint32 = 36
ENAMETOOLONG uint32 = 37
ENETDOWN uint32 = 38
ENETRESET uint32 = 39
ENETUNREACH uint32 = 40
ENFILE uint32 = 41
ENOBUFS uint32 = 42
ENODEV uint32 = 43
ENOENT uint32 = 44
ENOEXEC uint32 = 45
ENOLCK uint32 = 46
ENOLINK uint32 = 47
ENOMEM uint32 = 48
ENOMSG uint32 = 49
ENOPROTOOPT uint32 = 50
ENOSPC uint32 = 51
ENOSYS uint32 = 52
ENOTCONN uint32 = 53
ENOTDIR uint32 = 54
ENOTEMPTY uint32 = 55
ENOTRECOVERABLE uint32 = 56
ENOTSOCK uint32 = 57
ENOTSUP uint32 = 58
ENOTTY uint32 = 59
ENXIO uint32 = 60
EOVERFLOW uint32 = 61
EOWNERDEAD uint32 = 62
EPERM uint32 = 63
EPIPE uint32 = 64
EPROTO uint32 = 65
EPROTONOSUPPORT uint32 = 66
EPROTOTYPE uint32 = 67
ERANGE uint32 = 68
EROFS uint32 = 69
ESPIPE uint32 = 70
ESRCH uint32 = 71
ESTALE uint32 = 72
ETIMEDOUT uint32 = 73
ETXTBSY uint32 = 74
EXDEV uint32 = 75
ENOTCAPABLE uint32 = 76
const (
ESUCCESS Errno = 0
E2BIG Errno = 1 /* Arg list too long */
EACCES Errno = 2 /* Permission denied */
EADDRINUSE Errno = 3 /* Address already in use */
EADDRNOTAVAIL Errno = 4 /* Cannot assign requested address */
EAFNOSUPPORT Errno = 5 /* Address family not supported by protocol */
EAGAIN Errno = 6 /* Try again */
EALREADY Errno = 7 /* Operation already in progress */
EBADF Errno = 8 /* Bad file number */
EBADMSG Errno = 9
EBUSY Errno = 10
ECANCELED Errno = 11
ECHILD Errno = 12
ECONNABORTED Errno = 13
ECONNREFUSED Errno = 14
ECONNRESET Errno = 15
EDEADLK Errno = 16
EDESTADDRREQ Errno = 17
EDOM Errno = 18
EDQUOT Errno = 19
EEXIST Errno = 20
EFAULT Errno = 21
EFBIG Errno = 22
EHOSTUNREACH Errno = 23
EIDRM Errno = 24
EILSEQ Errno = 25
EINPROGRESS Errno = 26
EINTR Errno = 27
EINVAL Errno = 28 /* Invalid argument */
EIO Errno = 29
EISCONN Errno = 30
EISDIR Errno = 31
ELOOP Errno = 32
EMFILE Errno = 33
EMLINK Errno = 34
EMSGSIZE Errno = 35
EMULTIHOP Errno = 36
ENAMETOOLONG Errno = 37
ENETDOWN Errno = 38
ENETRESET Errno = 39
ENETUNREACH Errno = 40
ENFILE Errno = 41
ENOBUFS Errno = 42
ENODEV Errno = 43
ENOENT Errno = 44
ENOEXEC Errno = 45
ENOLCK Errno = 46
ENOLINK Errno = 47
ENOMEM Errno = 48
ENOMSG Errno = 49
ENOPROTOOPT Errno = 50
ENOSPC Errno = 51
ENOSYS Errno = 52
ENOTCONN Errno = 53
ENOTDIR Errno = 54
ENOTEMPTY Errno = 55
ENOTRECOVERABLE Errno = 56
ENOTSOCK Errno = 57
ENOTSUP Errno = 58
ENOTTY Errno = 59
ENXIO Errno = 60
EOVERFLOW Errno = 61
EOWNERDEAD Errno = 62
EPERM Errno = 63
EPIPE Errno = 64
EPROTO Errno = 65
EPROTONOSUPPORT Errno = 66
EPROTOTYPE Errno = 67
ERANGE Errno = 68
EROFS Errno = 69
ESPIPE Errno = 70
ESRCH Errno = 71
ESTALE Errno = 72
ETIMEDOUT Errno = 73
ETXTBSY Errno = 74
EXDEV Errno = 75
ENOTCAPABLE Errno = 76
)

var errnoToString = [...]string{
ESUCCESS: "ESUCCESS",
E2BIG: "E2BIG",
EACCES: "EACCES",
EADDRINUSE: "EADDRINUSE",
EADDRNOTAVAIL: "EADDRNOTAVAIL",
EAFNOSUPPORT: "EAFNOSUPPORT",
EAGAIN: "EAGAIN",
EALREADY: "EALREADY",
EBADF: "EBADF",
EBADMSG: "EBADMSG",
EBUSY: "EBUSY",
ECANCELED: "ECANCELED",
ECHILD: "ECHILD",
ECONNABORTED: "ECONNABORTED",
ECONNREFUSED: "ECONNREFUSED",
ECONNRESET: "ECONNRESET",
EDEADLK: "EDEADLK",
EDESTADDRREQ: "EDESTADDRREQ",
EDOM: "EDOM",
EDQUOT: "EDQUOT",
EEXIST: "EEXIST",
EFAULT: "EFAULT",
EFBIG: "EFBIG",
EHOSTUNREACH: "EHOSTUNREACH",
EIDRM: "EIDRM",
EILSEQ: "EILSEQ",
EINPROGRESS: "EINPROGRESS",
EINTR: "EINTR",
EINVAL: "EINVAL",
EIO: "EIO",
EISCONN: "EISCONN",
EISDIR: "EISDIR",
ELOOP: "ELOOP",
EMFILE: "EMFILE",
EMLINK: "EMLINK",
EMSGSIZE: "EMSGSIZE",
EMULTIHOP: "EMULTIHOP",
ENAMETOOLONG: "ENAMETOOLONG",
ENETDOWN: "ENETDOWN",
ENETRESET: "ENETRESET",
ENETUNREACH: "ENETUNREACH",
ENFILE: "ENFILE",
ENOBUFS: "ENOBUFS",
ENODEV: "ENODEV",
ENOENT: "ENOENT",
ENOEXEC: "ENOEXEC",
ENOLCK: "ENOLCK",
ENOLINK: "ENOLINK",
ENOMEM: "ENOMEM",
ENOMSG: "ENOMSG",
ENOPROTOOPT: "ENOPROTOOPT",
ENOSPC: "ENOSPC",
ENOSYS: "ENOSYS",
ENOTCONN: "ENOTCONN",
ENOTDIR: "ENOTDIR",
ENOTEMPTY: "ENOTEMPTY",
ENOTRECOVERABLE: "ENOTRECOVERABLE",
ENOTSOCK: "ENOTSOCK",
ENOTSUP: "ENOTSUP",
ENOTTY: "ENOTTY",
ENXIO: "ENXIO",
EOVERFLOW: "EOVERFLOW",
EOWNERDEAD: "EOWNERDEAD",
EPERM: "EPERM",
EPIPE: "EPIPE",
EPROTO: "EPROTO",
EPROTONOSUPPORT: "EPROTONOSUPPORT",
EPROTOTYPE: "EPROTOTYPE",
ERANGE: "ERANGE",
EROFS: "EROFS",
ESPIPE: "ESPIPE",
ESRCH: "ESRCH",
ESTALE: "ESTALE",
ETIMEDOUT: "ETIMEDOUT",
ETXTBSY: "ETXTBSY",
EXDEV: "EXDEV",
ENOTCAPABLE: "ENOTCAPABLE",
}
22 changes: 11 additions & 11 deletions wasi/wasi.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,14 @@ func (w *WASIEnvirnment) randUnusedFD() uint32 {
}
}

func (w *WASIEnvirnment) fd_prestat_get(ctx *wasm.HostFunctionCallContext, fd uint32, bufPtr uint32) (err uint32) {
func (w *WASIEnvirnment) fd_prestat_get(ctx *wasm.HostFunctionCallContext, fd uint32, bufPtr uint32) (err Errno) {
if _, ok := w.opened[fd]; !ok {
return EBADF
}
return ESUCCESS
}

func (w *WASIEnvirnment) fd_prestat_dir_name(ctx *wasm.HostFunctionCallContext, fd uint32, pathPtr uint32, pathLen uint32) (err uint32) {
func (w *WASIEnvirnment) fd_prestat_dir_name(ctx *wasm.HostFunctionCallContext, fd uint32, pathPtr uint32, pathLen uint32) (err Errno) {
f, ok := w.opened[fd]
if !ok {
return EINVAL
Expand All @@ -163,7 +163,7 @@ func (w *WASIEnvirnment) fd_prestat_dir_name(ctx *wasm.HostFunctionCallContext,
return ESUCCESS
}

func (w *WASIEnvirnment) fd_fdstat_get(ctx *wasm.HostFunctionCallContext, fd uint32, bufPtr uint32) (err uint32) {
func (w *WASIEnvirnment) fd_fdstat_get(ctx *wasm.HostFunctionCallContext, fd uint32, bufPtr uint32) (err Errno) {
if _, ok := w.opened[fd]; !ok {
return EBADF
}
Expand All @@ -173,7 +173,7 @@ func (w *WASIEnvirnment) fd_fdstat_get(ctx *wasm.HostFunctionCallContext, fd uin

func (w *WASIEnvirnment) path_open(ctx *wasm.HostFunctionCallContext, fd, dirFlags, pathPtr, pathLen, oFlags uint32,
fsRightsBase, fsRightsInheriting uint64,
fdFlags, fdPtr uint32) (errno uint32) {
fdFlags, fdPtr uint32) (errno Errno) {
dir, ok := w.opened[fd]
if !ok || dir.fileSys == nil {
return EINVAL
Expand All @@ -200,7 +200,7 @@ func (w *WASIEnvirnment) path_open(ctx *wasm.HostFunctionCallContext, fd, dirFla
return ESUCCESS
}

func (w *WASIEnvirnment) fd_write(ctx *wasm.HostFunctionCallContext, fd uint32, iovsPtr uint32, iovsLen uint32, nwrittenPtr uint32) (err uint32) {
func (w *WASIEnvirnment) fd_write(ctx *wasm.HostFunctionCallContext, fd uint32, iovsPtr uint32, iovsLen uint32, nwrittenPtr uint32) (err Errno) {
var writer io.Writer

switch fd {
Expand Down Expand Up @@ -231,7 +231,7 @@ func (w *WASIEnvirnment) fd_write(ctx *wasm.HostFunctionCallContext, fd uint32,
return ESUCCESS
}

func (w *WASIEnvirnment) fd_read(ctx *wasm.HostFunctionCallContext, fd uint32, iovsPtr uint32, iovsLen uint32, nreadPtr uint32) (err uint32) {
func (w *WASIEnvirnment) fd_read(ctx *wasm.HostFunctionCallContext, fd uint32, iovsPtr uint32, iovsLen uint32, nreadPtr uint32) (err Errno) {
var reader io.Reader

switch fd {
Expand Down Expand Up @@ -262,7 +262,7 @@ func (w *WASIEnvirnment) fd_read(ctx *wasm.HostFunctionCallContext, fd uint32, i
return ESUCCESS
}

func (w *WASIEnvirnment) fd_close(ctx *wasm.HostFunctionCallContext, fd uint32) (err uint32) {
func (w *WASIEnvirnment) fd_close(ctx *wasm.HostFunctionCallContext, fd uint32) (err Errno) {
f, ok := w.opened[fd]
if !ok {
return EBADF
Expand All @@ -277,14 +277,14 @@ func (w *WASIEnvirnment) fd_close(ctx *wasm.HostFunctionCallContext, fd uint32)
return ESUCCESS
}

func args_sizes_get(ctx *wasm.HostFunctionCallContext, argcPtr uint32, argvPtr uint32) (err uint32) {
func args_sizes_get(ctx *wasm.HostFunctionCallContext, argcPtr uint32, argvPtr uint32) (err Errno) {
// not implemented yet
binary.LittleEndian.PutUint32(ctx.Memory.Buffer[argcPtr:], 0)
binary.LittleEndian.PutUint32(ctx.Memory.Buffer[argvPtr:], 0)
return 0
}

func args_get(*wasm.HostFunctionCallContext, uint32, uint32) (err uint32) {
func args_get(*wasm.HostFunctionCallContext, uint32, uint32) (err Errno) {
// not implemented yet
return
}
Expand All @@ -293,11 +293,11 @@ func proc_exit(*wasm.HostFunctionCallContext, uint32) {
// not implemented yet
}

func environ_sizes_get(*wasm.HostFunctionCallContext, uint32, uint32) (err uint32) {
func environ_sizes_get(*wasm.HostFunctionCallContext, uint32, uint32) (err Errno) {
// not implemented yet
return
}

func environ_get(*wasm.HostFunctionCallContext, uint32, uint32) (err uint32) {
func environ_get(*wasm.HostFunctionCallContext, uint32, uint32) (err Errno) {
return
}

0 comments on commit 5c23040

Please sign in to comment.