From 08067e274f08921cbb9d4d85afd6d67c7cf85ecd Mon Sep 17 00:00:00 2001 From: Achille Roussel Date: Fri, 10 Dec 2021 10:11:06 -0800 Subject: [PATCH 1/2] introduce wasi.Errno type and switch error codes to constants --- wasi/errno.go | 249 ++++++++++++++++++++++++++++++++++---------------- wasi/wasi.go | 22 ++--- 2 files changed, 182 insertions(+), 89 deletions(-) diff --git a/wasi/errno.go b/wasi/errno.go index 0815806923..42cd611dc5 100644 --- a/wasi/errno.go +++ b/wasi/errno.go @@ -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", + "E2BIG", + "EACCES", + "EADDRINUSE", + "EADDRNOTAVAIL", + "EAFNOSUPPORT", + "EAGAIN", + "EALREADY", + "EBADF", + "EBADMSG", + "EBUSY", + "ECANCELED", + "ECHILD", + "ECONNABORTED", + "ECONNREFUSED", + "ECONNRESET", + "EDEADLK", + "EDESTADDRREQ", + "EDOM", + "EDQUOT", + "EEXIST", + "EFAULT", + "EFBIG", + "EHOSTUNREACH", + "EIDRM", + "EILSEQ", + "EINPROGRESS", + "EINTR", + "EINVAL", + "EIO", + "EISCONN", + "EISDIR", + "ELOOP", + "EMFILE", + "EMLINK", + "EMSGSIZE", + "EMULTIHOP", + "ENAMETOOLONG", + "ENETDOWN", + "ENETRESET", + "ENETUNREACH", + "ENFILE", + "ENOBUFS", + "ENODEV", + "ENOENT", + "ENOEXEC", + "ENOLCK", + "ENOLINK", + "ENOMEM", + "ENOMSG", + "ENOPROTOOPT", + "ENOSPC", + "ENOSYS", + "ENOTCONN", + "ENOTDIR", + "ENOTEMPTY", + "ENOTRECOVERABLE", + "ENOTSOCK", + "ENOTSUP", + "ENOTTY", + "ENXIO", + "EOVERFLOW", + "EOWNERDEAD", + "EPERM", + "EPIPE", + "EPROTO", + "EPROTONOSUPPORT", + "EPROTOTYPE", + "ERANGE", + "EROFS", + "ESPIPE", + "ESRCH", + "ESTALE", + "ETIMEDOUT", + "ETXTBSY", + "EXDEV", + "ENOTCAPABLE", +} diff --git a/wasi/wasi.go b/wasi/wasi.go index 7c97db99ae..469d290646 100644 --- a/wasi/wasi.go +++ b/wasi/wasi.go @@ -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 @@ -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 } @@ -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 @@ -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 { @@ -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 { @@ -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 @@ -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 } @@ -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 } From 33ee4e4a717150ed8d0ce70f6c826e3d72b430be Mon Sep 17 00:00:00 2001 From: Achille Roussel Date: Fri, 10 Dec 2021 10:14:08 -0800 Subject: [PATCH 2/2] use named indexes in wasi.errnoToString --- wasi/errno.go | 154 +++++++++++++++++++++++++------------------------- 1 file changed, 77 insertions(+), 77 deletions(-) diff --git a/wasi/errno.go b/wasi/errno.go index 42cd611dc5..7f5d1558b0 100644 --- a/wasi/errno.go +++ b/wasi/errno.go @@ -95,81 +95,81 @@ const ( ) var errnoToString = [...]string{ - "ESUCCESS", - "E2BIG", - "EACCES", - "EADDRINUSE", - "EADDRNOTAVAIL", - "EAFNOSUPPORT", - "EAGAIN", - "EALREADY", - "EBADF", - "EBADMSG", - "EBUSY", - "ECANCELED", - "ECHILD", - "ECONNABORTED", - "ECONNREFUSED", - "ECONNRESET", - "EDEADLK", - "EDESTADDRREQ", - "EDOM", - "EDQUOT", - "EEXIST", - "EFAULT", - "EFBIG", - "EHOSTUNREACH", - "EIDRM", - "EILSEQ", - "EINPROGRESS", - "EINTR", - "EINVAL", - "EIO", - "EISCONN", - "EISDIR", - "ELOOP", - "EMFILE", - "EMLINK", - "EMSGSIZE", - "EMULTIHOP", - "ENAMETOOLONG", - "ENETDOWN", - "ENETRESET", - "ENETUNREACH", - "ENFILE", - "ENOBUFS", - "ENODEV", - "ENOENT", - "ENOEXEC", - "ENOLCK", - "ENOLINK", - "ENOMEM", - "ENOMSG", - "ENOPROTOOPT", - "ENOSPC", - "ENOSYS", - "ENOTCONN", - "ENOTDIR", - "ENOTEMPTY", - "ENOTRECOVERABLE", - "ENOTSOCK", - "ENOTSUP", - "ENOTTY", - "ENXIO", - "EOVERFLOW", - "EOWNERDEAD", - "EPERM", - "EPIPE", - "EPROTO", - "EPROTONOSUPPORT", - "EPROTOTYPE", - "ERANGE", - "EROFS", - "ESPIPE", - "ESRCH", - "ESTALE", - "ETIMEDOUT", - "ETXTBSY", - "EXDEV", - "ENOTCAPABLE", + 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", }